Reputation: 45
Good day!
I have some problem with mysql syntax. I need to select only those name that have maximum SID
more than, input value 'count'
I tried to do it with pymysql like:
SELECT `name` FROM `files` WHERE
SELECT `sid` FROM `origin_files` WHERE `name` LIKE %s
ORDER BY `sid` DESC LIMIT 1 >= %s",(name, count);
Mysql said that there error in syntax.
desc files:
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(50) | NO | | NULL | |
| sid | int(4) | NO | | NULL | |
| path | varchar(200) | NO | PRI | NULL | |
+-------+--------------+------+-----+---------+-------+
desc origin_files:
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name | varchar(50) | NO | | NULL | |
| sid | int(4) | NO | | NULL | |
| path | varchar(200) | NO | PRI | NULL | |
+-------+--------------+------+-----+---------+-------+
I would be grateful if you help solve this problem.
Upvotes: 2
Views: 57
Reputation: 158
SELECT field
FROM TABLE_NAME
WHERE field IN
(SELECT field
FROM TABLE_NAME
WHERE field LIKE '%s'
ORDER BY field DESC);
Upvotes: 1