Mysql query issue, case when column is not null insert row - mysql

This is my query :
SELECT vehicle,
CASE
WHEN vehicle IS NOT NULL
THEN (INSERT INTO tbl_vehicle_on_user (vehicle, userid) values
(SELECT `vehicle` FROM `tbl_missions` WHERE `id` = 4 ), (SELECT `id` FROM `tbl_users` WHERE `id` = 12))
FROM tbl_missions WHERE id = 4;
I need to insert a row to tbl_vehicle_on_user when vehicle is not null on id 4.
When i execute this query i receive this error from mysql workbench,
01:24:49 SELECT vehicle, CASE WHEN vehicle IS NOT NULL THEN (INSERT INTO tbl_vehicle_on_user (vehicle, userid) values (SELECT vehicle FROM tbl_missions WHERE id = 4 ), (SELECT id FROM tbl_users WHERE id = 12)) FROM tbl_missions WHERE id = 4 Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO tbl_vehicle_on_user (vehicle, userid) values (SELECT vehicle FROM `tbl_mi' at line 4 0.000 sec
And i get a red line under 'INTO' when i hover over it, it says 'Syntax error, unexpected INTO, expecting ('.
I don't know what it means i tried to search the web but couldn't find anything if you know how to fix this i will appreciate it if you answer my question :)
THANKS!!

If you already know the userid should be 12, then just use 12 instead of SELECT id FROM tbl_users WHERE id = 12. Here is a valid insert-select statement.
INSERT INTO tbl_vehicle_on_user (vehicle, userid)
SELECT `vehicle`, 12 userid
FROM `tbl_missions`
WHERE `id` = 4 and vehicle is not null;

Related

mysql multiple Insert where select many column from another table

I have the following data:
table1:
id common_criteria member_id
1 some 1000
2 some1 500
3 some 100
4 some 200
3 some1 2000
table2
id member_id
The id columns are AUTOINCREMENT PRIMARY KEY on both tables
I need to insert into table2 three new rows (in this case) where common_criteria from table1 = 'some'
Expected result for table2:
id member_id
1 1000
2 100
3 200
I have tried:
INSERT into table2 (`member_id`)
VALUES (
SELECT `member_id` FROM table1 WHERE common_criteria = 'some'
)
But I get a syntax error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'SELECT member_id FROM table1 WHERE common_criteria = 'some')'
at line 1
You shouldn't need the VALUES part when you want to get the results from a SELECT. Instead you can simplify it to this:
INSERT into table2 (`member_id`)
SELECT `member_id`
FROM table1
WHERE common_criteria = 'some'
and it will insert all the rows returned by the SELECT.
Additional note (since people often ask this): If you had a scenario where some of the values are static every time, then you can do that too quite easily:
INSERT into table2 (`member_id`, `some_other_field`)
SELECT `member_id`, 'Static Text'
FROM table1
WHERE common_criteria = 'some'
In this case "Static Text" will be inserted every time into every row, no matter how many rows are returned (and inserted) as a result of the SELECT.
You can just do it like this:
INSERT INTO table2 (`member_id`)
SELECT `member_id` FROM table1 WHERE common_criteria = 'some'

PHP SQL IF NOT EXISTS won't work

I've got a question about SQL (php), I want to INSERT data in my table. But I want to use the IF NOT EXIST value.
What I've tried:
INSERT INTO vrienden (id, userid, vriendmetid, accepted) VALUES (null, '1', '20', '0') WHERE NOT EXISTS (SELECT * FROM vrienden WHERE userid='1' AND vriendmetid='20')
I'm not sure what's wrong, because I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE NOT EXISTS (SELECT * FROM vrienden WHERE userid='1' AND vriendmetid='20')' at line 1
Thanks.
You want insert . . . select, not insert . . . values:
INSERT INTO vrienden (id, userid, vriendmetid, accepted)
SELECT x.*
FROM (select null as id, '1' as userid, '20' as vriendmetid, '0' as accepted) x
WHERE NOT EXISTS (SELECT 1 FROM vrienden v WHERE v.userid = x.userid AND v.vriendmetid = x.vriendmetid);
However, you probably shouldn't be doing this in the INSERT. Instead, create a unique index/constraint:
create unique index unq_vrienden_userid_vriendmetid on vrienden(userid, vriendmetid);
This way, the database will ensure uniqueness of the columns, so your application does not have to.

SQL Transaction ELSE IF with SELECT

I am trying to create a query, that will check if it exist in a table, if it exist, it will just add one to quantity qty else it will insert from a selecting from other table, in my case temp_sales
here's my sql so far.
SET
TRANSACTION ISOLATION LEVEL SERIALIZABLE; BEGIN TRANSACTION; IF EXISTS (
SELECT
pid
FROM
temp_sales
WHERE
barcode = '4800556410652'
) BEGIN
UPDATE
temp_sales
SET
qty = qty + 1
WHERE
barcode = '4800556410652' END ELSE INSERT INTO temp_sales (
0,
(
SELECT
products.ID,
products.product_sprice as price,
1,
1 * price,
'4800556410652',
'101',
'admin'
WHERE
barcode = '4800556410652'
)
) END COMMIT TRANSACTION;
I have been following this stackoverflow answer from UPDATE if exists else INSERT in SQL Server 2008 but I have no luck.
MySQL error #104 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TRANSACTION' at line 1
what did I do wrong?
EDIT
PL: vb.net
DB: MySQL(xampp)
Thanks for the comments, my bad. I was following a SQL server query instead of focusing on MySQL query.
But here's the answer after a few additional searches (of course with the help of the commentators) - thanks
Answer:
INSERT into temp_sales (
ID, pid, price, qty, total, barcode,
pos_id, teller
)
SELECT
'0',
ID as pid,
products.product_sprice,
1,
products.product_sprice,
#barcode,
'101',
'admin'
FROM
products
WHERE
barcode = #barcode ON DUPLICATE KEY
UPDATE
qty = qty + 1,
total = price * qty

Using Unique IDs as Input

I have 2 tables.
Table Users has unique USERNAME values.
Table Roles has unique ROLENAME values.
I need to write a script that joins the 2 in a USER_TO_ROLE table:
INSERT INTO USER_TO_ROLE
(user_id,
role_id)
VALUES
(SELECT id FROM Users WHERE username=#username LIMIT 1,
SELECT id FROM Roles WHERE rolename=#rolename LIMIT 1);
MySQL Workbench gives Error Code 1064:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select id from Users where username=#username LIMIT 1, select record_id' at line 3
Is there a way to write that so that I don't have to make 2 separate database calls, writing the id values down, before making the insert?
RESOLVED
Using zerkms's example, I wrote this SQL:
INSERT INTO USER_TO_ROLE
(user_id,
role_id)
SELECT u.id, r.id
FROM Users u JOIN Roles r
WHERE u.username=#username and r.rolename=#rolename;
I had to take VALUES out for the INSERT statement to work.
You cannot have 2 SELECT clauses in INSERT INTO ... SELECT query.
You may rewrite your query to
INSERT INTO USER_TO_ROLE
(user_id,
role_id)
SELECT
(SELECT id FROM Users WHERE username=#username LIMIT 1),
(SELECT id FROM Roles WHERE rolename=#rolename LIMIT 1)
That way you would have a single SELECT with 2 nested queries correspondingly.

Mysql IF EXISTS THEN

So--i'm having an issue with my code. I'm testing it directly in the console and getting an "syntax error"
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'IF EXISTS(SELECT * FROM user_inventory WHERE resource_id = '6'
AND uid ='1') T' at line 1
IF EXISTS(SELECT * FROM user_inventory WHERE resource_id = '6' AND uid ='1')
THEN UPDATE user_inventory SET resource_count = resource_count+1 WHERE resource_id = 6 AND uid = 1
ELSE INSERT INTO user_inventory(uid, resource_id, resource_count) VALUES (1, 6, 1);
I've never used the IF EXISTS clause before... So I'm not sure what i've done wrong.
The SQL IF statement can only be used within a stored program. You can't use it within a generic SQL context as you have attempted.
However, you do not need to use IF here:
Define a suitable uniqueness constraint on your user_inventory table:
ALTER TABLE user_inventory ADD UNIQUE (uid, resource_id)
Use INSERT ... ON DUPLICATE KEY UPDATE:
INSERT INTO user_inventory
(uid, resource_id, resource_count)
VALUES
(1, 6, 1)
ON DUPLICATE KEY UPDATE
resource_count = resource_count + 1