MySQL populating linked tables - mysql

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;

Related

How do I check two different conditions for one column in an INSERT INTO SELECT block?

database noob so please bear with me. Writing in Oracle MySql.
I have a block of code which notably is meant to add rows to DWCUST but change invalid gender values coming from a2custbris to valid ones by comparing them to the genderspelling table. I wrote the select code in the brackets to do this and it works. However, certain gender values are null in a2custbris, I want these to be written as 'U' in DWCUST. So how can I do both?
Here's the code:
INSERT INTO DWCUST (DWCUSTID, DWSOURCEIDBRIS, DWSOURCEIDMELB, FIRSTNAME, SURNAME, GENDER, PHONE, POSTCODE, CITY, STATE, CUSTCATNAME)
SELECT dwcustSeq.nextval, cb.custid, Null, cb.fname, cb.sname, (select trim(gs.NEW_VALUE) FROM genderspelling gs WHERE upper(gs.INVALID_VALUE) = upper(cb.GENDER)), cb.phone, cb.postcode, cb.city, cb.state, cc.custcatname
FROM a2custbris cb
NATURAL JOIN a2custcategory cc
WHERE cb.rowid IN (SELECT source_rowid FROM A2ERROREVENT where filterid = 7);
Any help is creating appreciated!
Use a CASE expression:
INSERT INTO DWCUST (DWCUSTID, DWSOURCEIDBRIS, DWSOURCEIDMELB, FIRSTNAME, SURNAME,
GENDER, PHONE, POSTCODE, CITY, STATE, CUSTCATNAME)
SELECT
dwcustSeq.nextval, cb.custid, Null, cb.fname, cb.sname,
CASE WHEN cb.gender IS NULL THEN 'U'
ELSE (select trim(gs.NEW_VALUE) FROM genderspelling gs
WHERE upper(gs.INVALID_VALUE) = upper(cb.GENDER))
END,
cb.phone, cb.postcode, cb.city, cb.state, cc.custcatname
FROM a2custbris cb
NATURAL JOIN a2custcategory cc
WHERE cb.rowid IN (SELECT source_rowid FROM A2ERROREVENT where filterid = 7);
If you want 'U' for every value that has no match in genderspelling, then use COALESCE instead:
INSERT INTO DWCUST (DWCUSTID, DWSOURCEIDBRIS, DWSOURCEIDMELB, FIRSTNAME, SURNAME,
GENDER, PHONE, POSTCODE, CITY, STATE, CUSTCATNAME)
SELECT
dwcustSeq.nextval, cb.custid, Null, cb.fname, cb.sname,
COALESCE(
(select trim(gs.NEW_VALUE) FROM genderspelling gs
WHERE upper(gs.INVALID_VALUE) = upper(cb.GENDER))
, 'U'),
cb.phone, cb.postcode, cb.city, cb.state, cc.custcatname
FROM a2custbris cb
NATURAL JOIN a2custcategory cc
WHERE cb.rowid IN (SELECT source_rowid FROM A2ERROREVENT where filterid = 7);

The server has returned this error message:Duplicate column name 'NULL' MySQL Error

I'm trying to restore a backup from another server to a new one but I got this error:
Error while executing this query:
CREATE algorithm=undefined definer=root#localhost sql security definer VIEW resellers ASSELECT resellers1.id AS id,
(
SELECT clienttypes.id_client_type AS id_client_type
FROM clienttypes
WHERE (
clienttypes.id_client_type = 64)) AS client_type,
resellers1.idreseller AS idReseller,
resellers1.login AS login,
resellers1.password AS password,
resellers1.type AS type,
resellers1.id_tariff AS id_tariff,
resellers1.callslimit AS callsLimit,
resellers1.clientslimit AS clientsLimit,
resellers1.tech_prefix AS tech_prefix,
resellers1.identifier AS identifier,
resellers1.fullname AS Fullname,
resellers1.address AS Address,
resellers1.city AS City,
resellers1.zipcode AS ZipCode,
resellers1.country AS Country,
resellers1.phone AS Phone,
resellers1.email AS Email,
resellers1.maxclients AS MaxClients,
resellers1.template_id AS template_id,
resellers1.taxid AS TaxID,
resellers1.type2 AS type2,
resellers1.language AS language
FROM resellers1
WHERE (
NOT(
EXISTS
(
SELECT clientsshared.id_reseller AS id_reseller
FROM clientsshared
WHERE ((
clientsshared.id_tariff = -(2))
AND (
resellers1.login = clientsshared.login)
AND (
resellers1.password = clientsshared.password)))))
UNION ALL
SELECT resellers2.id AS id,
(
SELECT clienttypes.id_client_type AS id_client_type
FROM clienttypes
WHERE (
clienttypes.id_client_type = 65)) AS client_type,
resellers2.idreseller AS idReseller,
resellers2.login AS login,
resellers2.password AS password,
resellers2.type AS type,
resellers2.id_tariff AS id_tariff,
resellers2.callslimit AS callsLimit,
resellers2.clientslimit AS clientsLimit,
resellers2.tech_prefix AS tech_prefix,
resellers2.identifier AS identifier,
resellers2.fullname AS Fullname,
resellers2.address AS Address,
resellers2.city AS City,
resellers2.zipcode AS ZipCode,
resellers2.country AS Country,
resellers2.phone AS Phone,
resellers2.email AS Email,
NULL AS NULL,
resellers2.template_id AS template_id,
resellers2.taxid AS TaxID,
resellers2.type2 AS type2,
resellers2.language AS language
FROM resellers2
UNION ALL
SELECT resellers3.id AS id,
(
SELECT clienttypes.id_client_type AS id_client_type
FROM clienttypes
WHERE (
clienttypes.id_client_type = 66)) AS client_type,
NULL AS NULL,
resellers3.login AS login,
resellers3.password AS password,
resellers3.type AS type,
resellers3.id_tariff AS id_tariff,
resellers3.callslimit AS callsLimit,
resellers3.clientslimit AS clientsLimit,
resellers3.tech_prefix AS tech_prefix,
resellers3.identifier AS identifier,
resellers3.fullname AS Fullname,
resellers3.address AS Address,
resellers3.city AS City,
resellers3.zipcode AS ZipCode,
resellers3.country AS Country,
resellers3.phone AS Phone,
resellers3.email AS Email,
NULL AS NULL,
NULL AS NULL,
resellers3.taxid AS TaxID,
resellers3.type2 AS type2,
resellers3.language AS language
FROM resellers3;
The server has returned this error message:
Duplicate column name 'NULL'
MySQL Error.
Any thoughts what the problem is and how to fix it?
Three times in your SQL statement you have the words NULL AS NULL, which isn't correct.
where (clienttypes.id_client_type = 66)) AS client_type,NULL AS NULL,resellers3.login AS login
Email AS Email,NULL AS NULL,NULL AS NULL,resellers3.TaxID AS TaxID
There are a couple of problems. NULL isn't likely to be a column name and you can't define more than one column with the same alias.

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.

Use of `if( ... = ... )` in mysql?

insert into foo_table (fname, lname, number)
values ('John', 'Doe', if(123 = 456));
For the above MySQL query, can somebody kindly explain what the if(123 = 456) is doing? I currently struggle to see an if statement without a body (i.e. if(condition){ // do something });
The query is syntactically not correct as per mysql version 8, The syntactically correct query is insert into foo_table (fname, lname, number) values ('John', 'Doe', if(123 = 456,1,2)). This will insert 1 if the condition (123 =456) is true, otherwise it will insert 2.

INSERT SELECT MAX connected with text

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