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;
Related
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
Can someone please tell me what is syntactically wrong with this query?
The error message returned is not helping at all.
select P.test_result_calc_category, a.NumberofPatients from (select distinct test_result_calc_category FROM a1c) P
left join
(select test_result_calc_category, count (distinct patient_id) as NumberofPatients from a1c group by test_result_calc_category) a
ON P.test_result_calc_category = a.test_result_calc_category
#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 'distinct patient_id) as NumberofPatients from a1c group by test_result_calc_cate' at line 5
Remove the space after count
count(distinct patient_id)
^----------here
See https://dev.mysql.com/doc/refman/8.0/en/function-resolution.html
You have a space after the function COUNT in COUNT (DISTINCT ...
There are rules about this. You should have no space before the ( unless you set the sql_mode=IGNORE_SPACE.
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.
so i get an error on the this sql query, but i can´t see my mistake myself:
SELECT group_members.group_id,
group_members.permissions,
group.group_name
FROM group_members,
group
WHERE group_members.group_id=group.group_id
AND group.group_id = 1
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 'group WHERE group_members.group_id=group.group_id AND group.group_id = 1
Thanks for your help!
Group is a reserved word in MySQL either enclose it in backticks "`" or better yet do not use it as a table name
SELECT group_members.group_id, group_members.permissions, `group`.group_name
FROM group_members, `group`
WHERE group_members.group_id=`group`.group_id
AND `group`.group_id = 1
Try this
SELECT group_members.group_id, group_members.permissions, `group`.group_name
FROM group_members, group
WHERE group_members.group_id=`group`.group_id
AND `group`.group_id = 1
I found a very similar quesiton on here copied the code changed as required and it won't run under MySQL:
SELECT
id_manufacturer,
id_partname,
id_model,
cnumber,
COUNT (*)
FROM
rpi_consumables
GROUP BY
id_manufacturer, id_partname, id_model, cnumber
HAVING COUNT(*) > 1
Anyone have any ideas as to why the syntax fails?
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 '*) from rpi_consumables group by
id_manufacturer, id_partname, id_mode' at line 6
There should be no space between COUNT and (*).
SELECT
id_manufacturer,
id_partname,
id_model,
cnumber,
COUNT(*) /* This is where the space needs to be removed */
FROM
rpi_consumables
GROUP BY
id_manufacturer, id_partname, id_model, cnumber
HAVING COUNT(*) > 1