Insert multiple rows from select result - mysql

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.

Related

how to insert specific data on specific id in sql

i have 2 query first one is
SELECT stdntCode
FROM dbo.CourseStudent
WHERE(Status = 'Active') AND (CrsID = 1) AND (GrpID = 8)
second one is insert
INSERT INTO StudentExam (ExamID, CrsID, stdntCode, [Degree], StudGrpID, IsAttend, Notes, dateexam, [date])
VALUES(4, 3, stdntCode, 8.0, 1, 1, N'', '2023-01-17', '2023-02-01');
i want to insert this data to my first query i want replace stdntCode in my insert query with stdntCode list from my first query
thank you
i try that query
insert into StudentExam (stdntCode ,ExamID, CrsID, [Degree], StudGrpID, IsAttend, Notes, dateexam, [date])
select (SELECT stdntCode
FROM dbo.CourseStudent
WHERE(Status = 'Active') AND (CrsID = 1) AND (GrpID = 8)) as IDClient,
(4, 3,0, 1, 1, N'', '2023-01-17', '2023-02-01');
You need to provide the column values as part of a single select statement, like the following:
insert into StudentExam (stdntCode ,ExamID, CrsID, [Degree], StudGrpID, IsAttend, Notes, dateexam, [date])
select stdntCode, 4, 3, 0, 1, 1, N'', '2023-01-17', '2023-02-01'
from dbo.CourseStudent
where Status = 'Active' and CrsID = 1 and GrpID = 8;

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;

Not have Subquery,But the mysql throw Subquery return more than 1 row

A standard insert MySQL query without any Subquery, but the MySQL tells me
Subquery return more than 1 row
INSERT INTO
db_novelV2.tbl_saler_todo
(
is_drive, customer_type, operator_id, STATUS, remark,
update_dt, receive_saler, src_id, come_dt, has_give_present,
TYPE, service_id, src_type, allot_saler, has_car_shuttle,
customer_id, session_id, arrive_dt, creater_id, leave_dt,
collector_id, store_id, allot_dt, add_dt
)
VALUES
(0, 0, 528, 0, '',NULL, 307,0, NOW(), 0, 1, 352,0,307, 0, 243465,2993333,NOW(), 528, NULL, 0,4, NOW(), NOW());
Check whether you have any INSERT triggers on your table, If so, disable any triggers before running your INSERT statement.

Duplicate entry with a one to many relation

In Contact class I have
Long contactId;
#OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<Location> locations;
In Location class, I have
Long locationId;
When I try to init database, I do
INSERT INTO contact(first_name, last_name, contact_sub_category_contactSubCategoryId) values ('Dan Alvarez', 'Bernal', 5);
INSERT INTO contact_locations(contact_contactId, locations_locationId) values (1, 5);
INSERT INTO contact(first_name, last_name, contact_sub_category_contactSubCategoryId) values ('Steph', 'Borduas', 5);
INSERT INTO contact_locations(contact_contactId, locations_locationId) values (2, 4);
INSERT INTO contact(first_name, last_name, contact_sub_category_contactSubCategoryId) values ('Luc', 'La Haye', 5);
INSERT INTO contact_locations(contact_contactId, locations_locationId) values (3, 9);
INSERT INTO contact(first_name, last_name, contact_sub_category_contactSubCategoryId) values ('George', 'Leblond', 5);
INSERT INTO contact_locations(contact_contactId, locations_locationId) values (4, 9);
Duplicate entry '9' for key 'UK_63147bdnwnn3oecl34tlxkkd4' Column
count doesn't match value count at row 1
It's like locations_locationId can't have same value.
A contact can have many location and a location can many contact.
Don't know if it need to put a many to many relation there, would like to avoid that if i can.
Edit
I tried to use
#JoinTable(
uniqueConstraints=#UniqueConstraint(columnNames={"locations_locationId","contact_contactId"})
)
but that create 3 index and don't fixe the problem
Edit 2: with many to many, that work.

Return column names where they meet a specific value in sql

I've a User Table which has "Access Role" Defined in the table itself.
Schema:
UserID, UserName, Access_1, Access_2, Access_3, .... Access_10
Here values for Access_Columns is 1 or 0
Objective: I need Users and their Allowed accesses in one column as 'Access_Allowed'.
UserId, Access_Allowed
1, (Access_1, Access_3, Access_4, ...)
2, (Access_2, Access_3, Access_5, Access_10, ...)
Regards,
Yugal
Thanks Strawberry... I've Used Concat_ws here...
select userid, username,
concat_ws(', ',
IF(Access1 = 1 ,'Access1', NULL),
IF(Access2 = 1 ,'Access2', NULL),
IF(Access3 = 1 ,'Access3', NULL),
IF(Access4 = 1 ,'Access4', NULL),
.
.
IF( AccessN = 1 ,'AccessN',NULL) as Access_Allowed
from UserTable
Thanks