I have two MySQL tables:
Group(gr_id, gr_name, gr_description, parent_id)
Group_has_User(User_id, Group_id)
I'm trying to execute the query:
SELECT group.gr_id, group.gr_name, group.gr_description, group.parent_id
FROM group, Group_has_User AS gu
WHERE (group.gr_id = gu.Group_id) AND gu.User_id = 1
It gives an 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 'group, Group_has_User AS gu WHERE (group.gr_id = gu.Group_id) AND gu.User_id = 1' at line 1
How should I write it correct?
group is a keyword in SQL. Enclose such names in backticks
FROM `group`, Group_has_User AS gu
group is a keyword in SQL. Try giving your tables more sensible names, or using:
SELECT g.gr_id, g.gr_name, g.gr_description, g.parent_id
FROM `group` g, Group_has_User AS gu
WHERE (g.gr_id = gu.Group_id) AND gu.User_id = 1
Try this. Remove the "AS" keyword after the table name Group_has_User and execute the query
Maybe you must write 'Group', not 'group'.
Related
This it the code I am trying to execute:
SELECT ID_K
FROM koncert,
programi
WHERE koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT (DISTINCT programi.Salla) = 2
It returns this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use
near 'DISTINCT programi.Salla)=2 LIMIT 0, 25' at line 4.
Tried to change different things but it still won't work .
You should use the count(DISTINCT programi.Salla
) and not count (..) ..remove space between COUNT and (...
SELECT koncert.ID_K
FROM koncert
INNER JOIN programi on koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT(DISTINCT programi.Salla) = 2
but you need also tablename for avoid ambiguity and use explicit join sintax too
First you should use qualified name for your column, when column name is same in both table
SELECT ID_K FROM
should be
SELECT programi.ID_K FROM
else, you will get ambiguous column error. Otherwise, your query looks fine except removing extra space when calling COUNT (#spencer already mentioned in comment)
Also, it is good practice to join your table using JOIN (or INNER JOIN, LEFT JOIN etc) keyword, which makes your query more clear and readable.
I have the following mysqli code:
SELECT aircraft.*, [users_storage].*
FROM aircraft_database aircraft,
`[users_storage]` userstorage
WHERE aircraft.aircraftid = userstorage.aircraft
AND userstorage.userid = '1'
where the tables used is [users_storage] and aircraft
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 '[users_storage].* FROM aircraft_database aircraft,
[users_storage] userstorag' at line 1
This occurs when using [ and ] in table names.
How can I make this query work?
If your table name is [users_storage] then you have to use backticks to properly delimit the name in the alias definition. Then simply use the alias in the SELECT clause:
SELECT aircraft.*, userstorage.*
FROM aircraft_database aircraft
JOIN `[users_storage]` userstorage
ON aircraft.aircraftid = userstorage.aircraft
WHERE userstorage.userid = '1'
If, on the other hand, you table name is users_storage, then you don't have to use backticks at all:
SELECT aircraft.*, userstorage.*
FROM aircraft_database aircraft
JOIN users_storage userstorage
ON aircraft.aircraftid = userstorage.aircraft
WHERE userstorage.userid = '1'
Note: Use modern, explicit instead of implicit JOIN syntax.
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.
The following query gives me an error in phpmyadmin. It looks syntactically correct to me, and the table/column names match up accordingly. I have tried a number of variations (quoting table names, using as, etc) with no luck.
SELECT *
FROM GROUP
INNER JOIN GROUP_MEMBER ON GROUP.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';
Error I'm getting:
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 INNER JOIN GROUP_MEMBER ON GROUP.group_id = GROUP_MEMBER.group_id WHERE ' at line 2
"group" is a sql-keyword, you need to surround it with backticks if you want to use it as tablename.
GROUP is a reserved word in SQL so it's a bad choice for a table name. If you surround it with backticks it might work but I'd really recommend changing that table name.
SELECT *
FROM `GROUP`
INNER JOIN GROUP_MEMBER ON `GROUP`.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';
This is not PHPMyAdmin-specfiic error. The problem you have is using a table name GROUP that matches a MySQL reserved word. If you insist on using such a problematic table name, you need to enclose it with backticks anywhere you might use it.
SELECT *
FROM `GROUP`
INNER JOIN GROUP_MEMBER ON `GROUP`.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';
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