我有两个SQL查询:
1)
SELECT DISTINCT item_id
FROM sh_itemviews WHERE login='".$login."'
ORDER by time DESC LIMIT 5"
2)
SELECT *
FROM sh_item WHERE id=itemIdFrom sh_itemviews "
所以我想做得更好,我正在尝试使用LEFT JOIN:
SELECT DISTINCT *
FROM sh_itemviews
LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id;
WHERE sh_itemviews.login='".$login."'
ORDER by sh_itemviews.time DESC LIMIT 5"
像结果一样,我有所有数据,其中sh_item.id = sh_itemviews.item_id;但忽略其他构造WHERE。 我做错了什么? 谢谢!
答案 0 :(得分:1)
您在WHERE
声明之前有分号。
"SELECT DISTINCT * FROM sh_itemviews LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id**;** WHERE sh_itemviews.login='".$login."' ORDER by sh_itemviews.time DESC LIMIT 5"
正确的是:
"SELECT DISTINCT *
FROM sh_itemviews
LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id
WHERE sh_itemviews.login='".$login."'
ORDER by sh_itemviews.time DESC
LIMIT 5"
答案 1 :(得分:0)
额外的半列是原因,请在下方找到更新的查询
SELECT DISTINCT *
FROM sh_itemviews
LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id
WHERE sh_itemviews.login='".$login."'
ORDER by sh_itemviews.time DESC LIMIT 5"
答案 2 :(得分:0)
你在哪里做错了语法 试试这个语法
SELECT DISTINCT *
FROM sh_itemviews
LEFT JOIN sh_item ON sh_item.id = sh_itemviews.item_id
WHERE sh_itemviews.login='".$login."'
ORDER by sh_itemviews.time DESC
LIMIT 5"