我有2个工作的MYSQL语句,我想合并到1个查询中:
来自mysql select statement with unique and maximum selects?我有:
$sql="SELECT field_a, max(dup_number) as dup FROM table1 GROUP BY field_a"
我的第二个问题是:
$sql="where contacts=0 ORDER BY date LIMIT 3"
所以一组记录看起来像是:
ID FIELD A DUP_NUMBER CONTACTS
1 text1 0 1 --
2 text2 0 3 --
3 text2 1 3 --
4 text2 2 3 --
5 text3 0 2 --
我想出了:
SELECT *
FROM `table1 `
where max(`DUP_NUMBER `) as dup and `CONTACTS`=1
GROUP BY `FIELD_A`
ORDER BY date
LIMIT 3
当我在phpmyadmin中测试时,我得到了
#1064 - You have an error in your SQL syntax.
有人能告诉我如何正确地重写这个吗?
提前致谢,
比尔
答案 0 :(得分:1)
试试这个。
SELECT *, max(DUP_NUMBER) as dup FROM table1 where CONTACTS=1 GROUP BY FIELD_A ORDER BY date LIMIT 3
答案 1 :(得分:-2)
问题在这里:
where max(`DUP_NUMBER `) as dup and `CONTACTS`=1
^^^^^^
你不能在where子句中使用别名,只能在字段列表中,例如:
SELECT max(DUP_NUMBER) as dup
HAVING dup=???
是你如何使用它。