what is wrong following mysql query? - mysql

INSERT INTO lm_empleavetypemodelmap(leavetypeid,userid)
VALUES(1682,"b0c6c81f-a20a-4daa-9038-831478d8e11b")
WHERE lm_empleavetypemodelmap.userid NOT IN
(SELECT lm_empleavetypemodelmap.userid
FROM lm_empleavetypemodelmap
WHERE lm_empleavetypemodelmap.leavetypeid = 1683)
[Err] 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 'WHERE lm_empleavetypemodelmap.userid NOT IN( select lm_empleavetypemodelmap.user' at line 1

You do not use where with values. Perhaps the stuff at the end is just accidental garbage and you just want:
INSERT INTO lm_empleavetypemodelmap(leavetypeid, userid)
SELECT 1682, 'b0c6c81f-a20a-4daa-9038-831478d8e11b';
Or, in what looks rather strange to me:
INSERT INTO lm_empleavetypemodelmap(leavetypeid, userid)
SELECT 1682, 'b0c6c81f-a20a-4daa-9038-831478d8e11b'
WHERE NOT EXISTS (SELECT 1 FROM lm_empleavetypemodelmap WHERE leavetypeid = 1683)
That is, insert 1682, if 1683 doesn't exist. Usually you care about the value being inserted, not the next value.

Related

Why does this query break when I include a second WHERE condition?

The query is
SELECT account_id, type_id, client_id, avail_balance
FROM accounts
WHERE open_emp_id
IN (SELECT emp_id
FROM employee
WHERE (branch_id = 1111) AND (job_id NOT LIKE ‘HD%’);
Without the second WHERE condition, the query works fine. With that condition, I get
ERROR 1064 (42000): 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 '%’))' at line 1

What is wrong with this MySQL command?

The query that is throwing an error for my MySQL DB is:
SELECT t1.GROUPNAME FROM user_group t0, group t1 WHERE ((t0.users_USERNAME =
?) AND (t1.GROUPNAME = t0.groups_GROUPNAME))
The error info is the following:
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 'group t1 WHERE
((t0.users_USERNAME = 'test') AND (t1.GROUPNAME = t0.groups_GROUP')
Okay so I know the problem is with the group t1 part. But I dont know what is wrong with it.
Click here to see that I have all the needed colums
Can any one find out what the problem could be here?
group is a reserved word in SQL. You should put quotes around it.
Some JPA providers do that automatically, whereas others don't ...

SQL execution error

I try this query
INSERT INTO shop.product(prod_id,model,desc) SELECT product.id_prod,prod_lang.name,product.ref from product left join product_lang on product_lang.id_prod = product.id_prod
However I got this error
SQL execution error # 1065.Response from the database:
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 product.id_prod,product_lang.name,product.ref from' at line 1
Two problems:
DESC is a reserved keyword. Use backquote (``) for desc.
Change prod_lang to product_lang in the query.
Solution:
INSERT INTO shop.product (prod_id,model,`desc`)
SELECT product.id_prod,product_lang.name,product.ref
from product left join
product_lang on product_lang.id_prod = product.id_prod
Note:
It is a good practice to use backquotes for all columns eventhough it is not a reserved keyword.

SQL syntax error adding data opencart DB

Im looking to add all products that dont have attribute_id = 12 into oc_product_attribute,
But getting 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 '12) select ocp.product_id from oc_product ocp where ocp.product_id not in (SE' at line 1
insert into `oc_product_attribute` (ocp.product_id, 12)
select ocp.product_id from oc_product ocp where ocp.product_id not in (SELECT oca.`product_id`
FROM `oc_product_attribute` oca where oca.attribute_id = 12)
Am I missing something here, quite new to SQL.
Usually you have to enumerate columns in insert statements.
insert into `oc_product_attribute` (ocp.product_id, 12)
must be
insert into `oc_product_attribute` (ocp.product_id, some_column_name)
and I guess it may be attribute_id.
For more details, please see the reference manual -> http://dev.mysql.com/doc/refman/5.6/en/insert-select.html

Using COUNT and DISTINCT keywords together to count distinct values

I am writing the query to count the number of distinct values in the database table. So I wrote the query as:
select count ( distinct user_id ) as unique_users from 1_time_access;
I am getting error
ERROR 1064 (42000): 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 'distinct user_id ) as unique_users from 1_time_access' at line 1
Can anyone explain what am I missing?
Remove the space between count and the open parenthesis.
select count(distinct user_id) as unique_users from 1_time_access;