Simple SELECT SQL statement not working - mysql

SELECT user FROM members WHERE admin=1;
SELECT user FROM members WHERE id=1;
The first query does not work because there's an error in my syntax. The second works but they are virtually identical. Both 'admin' and 'id' are INT(11), but 'id' is the primary key and set to auto-increment. What's wrong here?
The error I receive is:
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 'admin=1' at line 1

When you use keywords or reserved words don't forget to add quotes or brackets.
SELECT [user] FROM members WHERE [admin]=1;
SELECT [user] FROM members WHERE id=1;

I apologize for the confusion everyone, my fault. I did this and it works.
SELECT user FROM members WHERE members.group=2;

Related

MySQL You can't specify target table for update in FROM clause

When I run this query
update Apply
set major='CSE'
where major='EE' and sID in (select sID from Student
where GPA >= all (select GPA from Student
where sID in (select sID from Apply
where major='EE')));
it returns an error: ERROR 1093 (HY000): You can't specify target table 'Apply' for update in FROM clause. But it succeeds in another MySQL.
if I add "as tmp" to the end of the code, it returns an 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 'as tmp' at line 6
Can somebody tell me the reason and how can I modify the code?
Thank you very much!
Problem: You're referencing the table Apply twice in two different contexts.
Solution: Give them different aliases, e.g. Apply as a1 and Apply as a2
(Maybe this also needs to be done for Student)

delete query for mysql using c

can anyone tell me the correct query to delete values from mysql db table,in my case the table name and id are accepted from the user and the row is deleted based on id.This is my query
sprintf(Query,"DELETE FROM ('%s') where id = (%d)",tb1,idt1) ;
/*table name is in form of string and id is int */
mysql_query(conn,Query);
You should remove parentheses around the table name:
sprintf(Query,"DELETE FROM '%s' where id = (%d)",tb1,idt1) ;
MySQL considers queries like this syntax errors:
delete from (mytable) where id=2;
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 '(mytable) where id=2' at line 1
(I'll assume that you know everything about SQL injection attacks, and that neither tb1 nor idt1 are constructed from user input in any shape or form).

Mysql syntax error during an update but code look fine

I have a strange issue with a Mysql Update and I'm not sure what is causing it. I suspect something is wrong with the table itself but the field causing the error appears to be defined the same as other fields in the table. I can recreate the error in phpMyAdmin on the SQL tab and also in php code. I am completely stumped.
The fields in the table are defined as follows:
bnumber is INT length=11
bname is VARCHAR length=60 Collation=latin1_swedish_ci
twittername is VARCHAR length=15 Collation=latin1_swedish_ci
desc is VARCHAR length=60 Collation=latin1_swedish_ci
This update statement works:
update tbl1 set bname='myName', twittername='myTweet' where bnumber=1;
this one gives me the error:
update tbl1 set bname='myName', twittername='myTweet', desc='test' where bnumber=1;
the error I get is:
#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 'desc='Main' where bnumber=1' at line 1.
I don't seem to have any issues selecting from the table or inserting to the table. Only update is giving me the error.
I appreciate any help.
Thanks!
desc is a reserved word in MySQL. You must quote it in backticks.
desc is a keyword. Escape it with backticks.
update tbl1
set bname='myName',
twittername='myTweet',
`desc`='test'
where bnumber=1;
desc is a reserved word in mysql
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
Updated your sql as below and it will work fine;
update tbl1 set bname='myName', twittername='myTweet', `desc`='test'
where bnumber=1;
you cant use desc as a field name.
change fieldname to desc1 and try.
desc is for oder by name desc

MySQL foreign key problem

I'm getting that error running on MySQL 5.5.8
Mysql2::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
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
EN' at line 6:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What could be wrong?
I suspect the problem is there is no FROM clause in your select statement.
Are you sure you can raise errors inside a query like that? I can't find it anywhere. I think the proper way would be to select a COUNT or EXISTS and return the result of that INTO a variable. Then, after the query, raise an error if the result doesn't meet your expectations.
Something like this:
SELECT count(id) INTO IDCOUNT FROM products WHERE id = NEW.brand_id;
Wouldn't it be better by the way to just add a real constraint? Or do you use a storage type that doesn't support that?

Modifing select query to delete query

The following select query works fine:
SELECT * FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
However, when I try to delete the rows retrieved here, it fails
DELETE FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
What is wrong here?
The error message is:
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 'job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LI' at line 1
You need to specify you are deleting from the alias table, so use:
DELETE job FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
i have tested the query in sql server. works fine but there is possible that the values you are deleting have some relationship with other table like PK and FK.
if they have then you have to delete the records from those tables too..........