【发布时间】:2018-03-29 00:36:15
【问题描述】:
a16s
id pic
1 1.jpg
2 2.jpg
3 3.jpg
4 4.jpg
a16s_like
id p_id u_id approve
1 1 2 0
2 1 1 1
3 1 5 1
4 1 6 1
5 1 7 0
6 2 2 0
7 2 3 0
8 2 1 1
9 4 4 0
10 4 3 1
11 4 2 1
$sql="SELECT A.id, A.pic
(select count(*) from a16s_like B where B.p_id = A.id) AS Ashow,
(select count(*) from a16s_like B where B.p_id = A.id and B.approve='0') AS Nshow,
(select count(*) from a16s_like B where B.p_id = A.id and B.approve='1') AS Yshow
FROM a16s A ORDER BY id DESC";
我用了3次approvic colume,有什么方法可以说服seach吗? 我的目的是
当 u_id=2 执行 select 时,我想得到
id pic Ashowocunt approve_0_count approve_1_count u_id2_approve
1 1.jpg 5 2 3 0
2 2.jpg 3 2 1 0
3 3.jpg 0 0 0 null
4. 4.jpg 3 0 3 0
u_id=3
id pic Ashowocunt approve_0_count approve_1_count u_id3_approve
1 1.jpg 0 0 0 null
2 2.jpg 0 1 0 0
3 3.jpg 0 0 0 null
4. 4.jpg 1 0 1 1
需要显示变量 u_id 的所有 a.id,就像 u_id=2 或 u_id=3(有 null)
【问题讨论】:
-
我看到了您的预期结果。我编辑我的答案。
-
感谢您的帮助。效果很好!我喜欢这段代码!
-
不客气,对不起,我第一次阅读时误解了您的问题。