我需要列出所选用户的结果主题以及如下所示的消息ID:
测试1(主题)
消息ID(我将添加此ID的链接)
(可以在消息表中为同一主题使用相同用户的多条记录)
-------------------------------------
TABLES
-------------------------------------
TABLE: users
id int (auto_increment)
user varchar
-------------------------------------
TABLE: topics
id int
user_id int (index)
topic varcahar
-------------------------------------
TABLE: messages
id int (auto_increment)
user_id int (index)
topic_id int (index)
message text
-------------------------------------
我确实试过这段代码,但我收到了错误。
SELECT users.id,users.user,topics.topic,messages.id FROM users,topics,messages WHERE messages.user_id=users.id and users.user='$_GET[user]' ORDER BY topics.topic
我该怎么做?
答案 0 :(得分:1)
SELECT u.id, u.user, t.topic, m.id
FROM topics t
LEFT JOIN users u ON u.id = t.user_id
LEFT JOIN messages m ON m.user_id = u.id
WHERE u.user = 'xyz'
ORDER BY t.topic
但你应该逃避$_GET[user]