I would like some help on this, i have a table like this
CREATE TABLE IF NOT EXISTS `items` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`code` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`unitPrice` decimal(8,2) NOT NULL,
`quantity` int(11) NOT NULL,
`totalSold` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
and after using DB::table('items')->get();
i would like to get the same result as if i run this sql command
"select `code`,(`unitPrice`*(`quantity`+`totalSold`)) as totalEarnIfsold from `items` order by `totalEarnIfsold` desc"
what i have been able to achieve without success ofc is this:
$items_all->sortBy([
$totalEarnIfsold= fn ($a) => $a->unitPrice *($a->quantity+$a->totalSold),
['totalEarnIfsold', 'desc'],
]);
So i need your help if you may ofc, and thanks
You can do in following way
First you need to calculate totalEarnIfsold with map function and after that you can easily sort with value.
$items_all = $items_all->map(function($item) {
$item->totalEarnIfsold = $item->unitPrice *($item->quantity + $item->totalSold);
return $item;
})->sortByDesc('totalEarnIfsold');
Please help. I'm not sure what is happening. I even tried to drop table but still unsuccessful.
CREATE TABLE 'customer' (
'customer_id' int NOT NULL AUTO_INCREMENT,
'type' varchar(45) NOT NULL,
'name' varchar(45) NOT NULL,
'cut_off' int NOT NULL,
PRIMARY KEY ('customer_id')
)
Error starting at line : 1 in command -
CREATE TABLE 'customer' (
'customer_id' int NOT NULL AUTO_INCREMENT,
'type' varchar(45) NOT NULL,
'name' varchar(45) NOT NULL,
'cut_off' tinyint NOT NULL,
PRIMARY KEY ('customer_id')
)
Error report -
ORA-00903: invalid table name
00903. 00000 - "invalid table name"
*Cause:
*Action:
Column and table names should be without quotes.
Refer to create table query https://docs.oracle.com/cd/B28359_01/server.111/b28310/tables003.htm#ADMIN11004
There are multiple issues in your code.
Names of table and columns should not be wrapped in single quotes.
AUTO_INCREMENT keyword is not allowed. It should be implemented differently in oracle.
NOT NULL is not needed on PK column. It is implicit.
Corrected code:
CREATE TABLE customer (
customer_id int GENERATED always as IDENTITY,
type varchar(45) NOT NULL,
name varchar(45) NOT NULL,
cut_off int NOT NULL,
primary key(customer_id)
);
I have to increase the counter and update the updated_at column. I used following code to increase the counter based on the click but I am not sure how to update date column.
Link::where('role', $id)->increment('counter');
It should be the default value of updated_at is ON UPDATE CURRENT_TIMESTAMP.
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Or,
`updated_at` TIMESTAMP NULL DEFAULT ON UPDATE CURRENT_TIMESTAMP,
I just migrated my app from mysql to postgres but when I try to insert a record in a specific table I get violates not-null constraint error:
ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, 1, 1, null, null, null, 2016-03-09 09:24:12.841891, 2012-12-31 23:00:00, 2012-12-31 23:00:00, null, null, f, null, f, XYZAssignment, null, null, null, null).
********** Error **********
ERROR: null value in column "id" violates not-null constraint
SQL state: 23502
Detail: Failing row contains (null, 1, 1, null, null, null, 2016-03-09 09:24:12.841891, 2012-12-31 23:00:00, 2012-12-31 23:00:00, null, null, f, null, f, XYZAssignment, null, null, null, null).
When I try to create the record using factory_girl:
#assignment = FactoryGirl.create(:assignment)
It builds this sql query:
INSERT INTO assignments(
id, account_id, l_id, viewed_at, accepted_at, declined_at,
expires_at, created_at, updated_at, decline_reason, decline_reason_text,
promotion, c_checked_at, forwardable, type, f_promo,
c_check_successful, c_check_api_result, c_check_human_result)
VALUES (null, 1, 1, null, null, null, '2016-03-09 09:24:12.841891', '2012-12-31 23:00:00', '2012-12-31 23:00:00', null, null, 'f', null, 'f', 'XYZAssignment', null, null, null, null);
This is the assignment factory:
FactoryGirl.define do
factory :assignment do
expires_at 24.hours.from_now
account
lead
end
end
this is the table description:
CREATE TABLE assignments(
id serial NOT NULL, account_id integer NOT NULL, l_id integer NOT NULL, viewed_at timestamp without time zone, accepted_at timestamp without time zone, declined_at timestamp without time zone, expires_at timestamp without time zone, created_at timestamp without time zone, updated_at timestamp without time zone, decline_reason character varying(16), decline_reason_text character varying(256), promotion boolean NOT NULL DEFAULT false, c_checked_at timestamp without time zone, forwardable boolean DEFAULT true, type character varying(64), f_promo boolean, c_check_successful boolean, c_check_api_result character varying(32), c_check_human_result character varying(32), CONSTRAINT assignments_pkey PRIMARY KEY (id)
) WITH (
OIDS=FALSE
);
Looks its not able to auto increment the id, any idea?
You have to skip id in the INSERT operation:
INSERT INTO assignments(account_id, l_id, ...)
VALUES
(1, 1, ...)
The id will automatically get the next sequence number, since it is an auto-increment field.
I have a table:
create table employee (
employee_id NUMBER NOT NULL,
name VARCHAR2(255) NOT NULL,
notes VARCHAR2(4000),
created_by varchar2(255) not null,
created_at date default sysdate not null,
updated_by varchar2(255) not null,
updated_at date default sysdate not null,
PRIMARY KEY(vendor_id)
);
so when I insert from SQL developer:
insert into employee(employee_id, name,notes) values(1,'xyz','test');
it auto populates create_by, created_at, updated_at and updated_by.
row gets inserted successfully.
Whereas if I try to insert using cx_Oracle module in python,
cursor.execute("INSERT INTO employee VALUES (:employee_id,:name,:notes)",
{
'employee_id' : max_value,
'name' : each_vendor,
'notes' : 'test'
}
)
it throws error saying not enough values.
Why do I get this error? How can I solve it?
The answer is very simple, and has nothing to do with python. Your 2 insert statements are very different.
In the 1st, you explicitly name the columns you intend to provide values for: (employee_id, name,notes). However, in the insert statement used from Python, you don't specify the 3 columns by name. As a result, your insert statement expects you to provide the values for all columns in the table.
The fix: explicitly name the 3 columns:
cursor.execute("INSERT INTO employee (employee_id, name, notes) VALUES (:employee_id,:name,:notes)",
{
'employee_id' : max_value,
'name' : each_vendor,
'notes' : 'test'
}
)