我是PHP和MySQL的新手,我无法理解JOIN
。我想,对我来说,问题在于实际理解查询的逻辑。我要做的是从名为“post”的表中选择所有状态更新,但只选择我“跟随”的用户的状态更新,然后按日期顺序显示它们。所以,我有两个数据库设置如下:
帖子
|post_id|user_id|post_body|date_upload|
| 1 | 4 | hey. | 01/2/2012 |
如下
|relation_id|user_id|followee_id|
| 1 | 4 | 2 |
有人可以解释我应该如何在语法和逻辑上进行设置吗?
谢谢!
答案 0 :(得分:5)
我想你会发现这会起作用:
SELECT
p.post_id,
p.user_id,
p.post_body,
p.date_upload
FROM
posts p,
follows f
WHERE
f.user_id = 4
AND
f.followee_id = p.user_id
ORDER BY
p.date_upload DESC
答案 1 :(得分:1)
试试这个
select posts.*
from posts
join follows on (follows.user_id = posts.user_id)
where followee_id = xxxx
答案 2 :(得分:-1)
客户“表”
CustomerID CustomerName
1 Alfreds Futterkiste
2 Ana Trujillo Emparedados y helados
3 Antonio Moreno Taquería
“订单”表:
OrderID CustomerID
10308 2
10309 37
10310 77
*********************** ANSWER *******************
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
客户名称 订单ID
Alfreds Futterkiste -
Ana Trujillo Emparedados和helados 10308
AntonioMorenoTaquería10365
- 10382
- 10351