INSERT SELECT MAX connected with text - mysql

I want to insert to type field max id from other table but I need to connect it with text information as "Created new user with id = "MAX(my_employee.id). Code that work but insert only id:
INSERT INTO my_logs (user_id, type, date)
SELECT '1', MAX(my_employee.id), '2013-05-28 23:52:07' FROM my_employee
I tried:
INSERT INTO my_logs (user_id, type, date)
SELECT '1',"Created new user with id =" MAX(my_employee.id),
'2013-05-28 23:52:07' FROM my_employee
and similar but nothing seems to work

Are you trying to do this?
INSERT INTO my_logs (user_id, type, date)
SELECT '1', concat('Created new user with id =', MAX(my_employee.id)),
'2013-05-28 23:52:07'
FROM my_employee;

This should work for you:
insert into my_logs
select 1, concat('Created new user with id = ', MAX(id)), '2013-05-28 23:52:07'
from my_employee
SQL Fiddle Demo

Related

I am receiving duplicate column name, but based on values. When i change the value, there is no error

I am trying to use this query, but when there are the same value in different columns, I receive this error:
1060 - Duplicate column name "123"
For example here:
INSERT INTO chiro(in_out,chirocov,chirocov2,chiroded,chiromet,
chirocovp,chirooop,chirooopmet,chirooopcp,
chirovisit,chirouse,chiromax,chirodedapply,
chironum1,chironum2)
SELECT * FROM (SELECT 'in', 'no','individual','123','123','20',
'213','21243','10','14','5','2000','yes',
'0','1') AS tmp
WHERE NOT EXISTS (SELECT in_out,chirocov,chirocov2,
chiroded,chiromet,chirocovp,chirooop,
chirooopmet,chirooopcp, chirovisit,chirouse,
chiromax,chirodedapply,chironum1,chironum2
FROM chiro
WHERE chirocov='no'
AND chirocov2='individual'
AND chiroded='123'
AND chiromet='123'
AND chirocovp='20'
AND chirooop='213'
AND chirooopmet='213'
AND chirooopcp='10'
AND chirovisit='14'
AND chirouse='5'
AND chiromax='2000'
AND chirodedapply='yes'
AND chironum1='0'
AND chironum2='1')
LIMIT 1
But when i change the value, there won"t be any errors. like:
INSERT INTO chiro(in_out,chirocov,chirocov2,chiroded,
chiromet,chirocovp,chirooop,chirooopmet,
chirooopcp, chirovisit,chirouse,chiromax,
chirodedapply,chironum1,chironum2)
SELECT * FROM (SELECT 'in', 'no','individual','123','231','20',
'213','21243','10','14','5','2000',
'yes', '0','1') AS tmp
WHERE NOT EXISTS (SELECT in_out,chirocov,chirocov2,
chiroded,chiromet,chirocovp,chirooop,
chirooopmet,chirooopcp, chirovisit,chirouse,
chiromax,chirodedapply,chironum1,chironum2
FROM chiro
WHERE chirocov='no'
AND chirocov2='individual'
AND chiroded='123'
AND chiromet='123'
AND chirocovp='20'
AND chirooop='213'
AND chirooopmet='213'
AND chirooopcp='10'
AND chirovisit='14'
AND chirouse='5'
AND chiromax='2000'
AND chirodedapply='yes'
AND chironum1='0'
AND chironum2='1')
LIMIT 1
Could you help me and let me know what I am doing wrong?
Just remove the outer SELECT from:
SELECT * FROM (SELECT 'in', 'no','individual','123','123','20',
'213','21243','10','14','5','2000','yes',
'0','1') AS tmp
because it retrieves all the unnamed columns from the inner select with names that are their values, so there are 2 columns with the same name 123.
See a simplified demo of the problem.
Use this:
INSERT INTO chiro(
in_out, chirocov, chirocov2, chiroded, chiromet, chirocovp, chirooop, chirooopmet, chirooopcp,
chirovisit, chirouse, chiromax, chirodedapply, chironum1, chironum2
)
SELECT 'in', 'no', 'individual', '123', '123', '20', '213', '21243', '10', '14', '5', '2000', 'yes', '0', '1'
WHERE NOT EXISTS(
SELECT in_out, chirocov, chirocov2, chiroded, chiromet, chirocovp, chirooop, chirooopmet, chirooopcp,
chirovisit, chirouse, chiromax, chirodedapply, chironum1, chironum2
FROM chiro
WHERE chirocov='no' AND chirocov2='individual' AND chiroded='123' AND chiromet='123' AND chirocovp='20'
AND chirooop='213' AND chirooopmet='213' AND chirooopcp='10' AND chirovisit='14' AND chirouse='5'
AND chiromax='2000' AND chirodedapply='yes' AND chironum1='0' AND chironum2='1'
)
Also LIMIT 1 is not necessary.

MySQL populating linked tables

I'm new to MySQL and as part of an exercise I have had to create a database for a flight reservation system, I'm trying to create a customer which means inserting a row into 3 linked tables. I'm trying to do this as a transaction but my syntax seems very clunky. Is there a better way to do this?
Here's my transaction (which does work) but I feel there must be a FAR more elegant way to do this:
START TRANSACTION;
SET #account_id = NULL;
INSERT INTO ezy_account (account_id, username, password)
VALUES (#account_id = #account_id, 'customer#mysite.com',AES_ENCRYPT('password1', 'encryptionkey'));
SET #payment_id = NULL;
INSERT INTO ezy_payment_details (payment_details_id, payment_type, account_id, card_number, name, valid_from, expiry)
VALUES (#payment_id = #payment_id, 1, (SELECT account_id FROM ezy_account WHERE username ='ngray#mysite.com'), '1234567890123456', 'chris cust', '2018-03-00', '2021-10-00');
INSERT INTO ezy_customer (customer_id, payment_details, title, first_name, last_name, email_address, address, address_continued, city, postcode, country, dialling_code, phone_number, account_id, easyjet_plus_number, preferred_airport1, preferred_airport2, preferred_airport3)
VALUES (NULL, (SELECT payment_details_id FROM ezy_payment_details WHERE card_number ='1234567890123456'), 1, 'chris', 'customer', 'customer#nmysite.com', '123 Road Street', NULL, 'london', 'SE1 4HG', 7, 7, '1898765432', (SELECT account_id FROM ezy_account WHERE username ='customer#mysite.com'), NULL, NULL, NULL, NULL);
COMMIT;

SQL IF exist date by day do increment update else insert data

How can I express the below statement as a SQL query ?
IF EXISTS (SELECT * FROM expense_history
WHERE user_id = 40
AND DATE_FORMAT(expense_history.created_date , '%Y-%m-%d') = '2018-06-02'
AND camp_id='80')
UPDATE expense_history
SET clicks = clicks + 1,
amount = amount + 1
WHERE user_id = 40
AND DATE_FORMAT(expense_history.created_date, '%Y-%m-%d') = '2018-06-02'
AND camp_id = '80'
ELSE
INSERT INTO expense_history (camp_id, created_date, amount, user_id)
VALUES ('80', '2018-06-02 12:12:12', '1', '40')
END IF;
I just want to do increment clicks and amount if is set by day, else I want to add new row.
This is very tricky in MySQL. You are storing a datetime but you want the date part to be unique.
Starting in MySQL 5.7.?, you can use computed columns for the unique constraint. Here is an example:
create table expense_history (
user_id int,
camp_id int,
amount int default 0,
clicks int default 1,
. . .
created_datetime datetime, -- note I changed the name
created_date date generated always as (date(created_datetime)),
unique (user_id, camp_id, created_datetime)
);
You can then do the work as:
INSERT INTO expense_history (camp_id, created_datetime, amount, user_id)
VALUES (80, '2018-06-02 12:12:12', 1, 40)
ON DUPLICATE KEY UPDATE
amount = COALESCE(amount + 1, 1),
clicks = COALESCE(clicks + 1, 1);
Earlier versions of MySQL don't support generated columns. Nor do they support functions on unique. But you can use a trick on a prefix index on a varchar to do what you want:
create table expense_history (
user_id int,
camp_id int,
amount int default 0,
clicks int default 1,
. . .
created_datetime varchar(19),
unique (created_datetime(10))
);
This has the same effect.
Another alternative is to store the date and the time in separate columns.
I presumed your database is mysql, because of DATE_FORMAT() function(and edited your question as to be).
So, by using such a mechanism below, you can do what you want,
provided that a COMPOSITE PRIMARY KEY for camp_id, amount, user_id columns :
SET #camp_id = 80,
#amount = 1,
#user_id = 40,
#created_date = sysdate();
INSERT INTO expense_history(camp_id,created_date,amount,user_id,clicks)
VALUES(#camp_id,#created_date,#amount,#user_id,ifnull(clicks,1))
ON DUPLICATE KEY UPDATE
amount = #amount + 1,
clicks = ifnull(clicks,0)+1;
SQL Fiddle Demo

Insert multiple rows from select result

For a database fix I have to perform I need to do an insert for every ID this query results;
SELECT Id FROM role WHERE id != 99 AND Id NOT IN (SELECT RoleId FROM permissionaction WHERE PermissionId = 19);
So it results some ID's which have to be added with this query.
INSERT INTO permissionaction (AccessLevel, PermissionId, ControllerActionId, Active, CreatedOn, ModifiedOn, CreatedById, ModifiedById, RoleId) VALUES (1, 19, 1, 1, Now(), Now(), 1, 1, *******ID HERE******);
I know it can be done with using mysql cursors but it's not a possibility here. Is there a way to add all of the resulting ID's in one query?
Thanks
Use INSERT...SELECT:
INSERT INTO permissionaction (AccessLevel, PermissionId, ControllerActionId, Active, CreatedOn, ModifiedOn, CreatedById, ModifiedById, RoleId)
SELECT 1, 19, 1, 1, Now(), Now(), 1, 1, Id
FROM role
WHERE id != 99
AND Id NOT IN (SELECT RoleId FROM permissionaction WHERE PermissionId = 19);
With INSERT...SELECT you can add values to the table permissionaction for each returned row of the SELECT query.

How to use validation when i use duplicate key update

When I Use duplicate key update
INSERT INTO test_table (name, age, date)
VALUES (Kevin, 20, now)
ON DUPLICATE KEY UPDATE name = VALUES(name),
age = VALUES(age),
date = VALUES(date)
I want to compare with existing value for validation
How can I do?
I have tried some method
where date < VALUES(date)
But all of method are syntax error
Thanks for reading
Try using CASE EXPRESSION :
INSERT INTO test_table (name, age, date)
VALUES (Kevin, 20, now)
ON DUPLICATE KEY
UPDATE name = CASE WHEN `date`< values(`date`) THEN VALUES(name) ELSE name end,
age= CASE WHEN `date`< values(`date`) THEN VALUES(age) ELSE age end,
`date`= CASE WHEN `date`< values(`date`) THEN VALUES(date) ELSE `date` end
INSERT INTO test_table (name, age, date)
VALUES (Kevin, 20, now)
ON DUPLICATE KEY UPDATE age = IF(`date` < values(`date`), values(age), age),
name = IF(`date` < values(`date`), values(name), name),
date = IF(`date` < values(`date`), values(date), date)