我对PHP和MySQL还不熟悉->我还不太擅长。
今天我遇到了这个问题。我有一个连接两个表“投票”和“故事”的查询
这里是:

SELECT stories.*, SUM(votes.vote_value) as 'total_votes'
FROM stories JOIN votes ON stories.id = votes.item_name WHERE stories.st_date >= DATE_SUB(NOW(), INTERVAL 32 DAY)
GROUP BY stories.id
ORDER BY total_votes ASC LIMIT 10

我需要修改它,这样它只从'stories'表中选择信息,其中field showing=1
一个简单的查询如下:
SELECT * FROM stories WHERE showing = 1

但是我不知道如何在连接两个数据库的第一个查询中实现它。

最佳答案

SELECT stories.*, SUM(votes.vote_value) as 'total_votes'
FROM stories, votes
WHERE stories.id = votes.item_name AND stories.showing = 1 AND stories.st_date >= DATE_SUB(NOW(), INTERVAL 32 DAY)
GROUP BY stories.id
ORDER BY total_votes ASC LIMIT 10

关于mysql - 无法弄清mysql查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8636059/

10-11 02:08