解释子查询na父查询

时间:2016-05-14 18:07:07

标签: mysql

子查询将在父查询和子查询将结果传递给父查询之前执行....但是在下面的父查询中使用子查询结果........... enter image description here

 $sth = $conn->query(
    "SELECT tab.AttDate AS Salman,(SELECT COUNT(*) FROM attendence_info WHERE AttStatus='P' AND AttDate=Salman) AS Monu,
    (SELECT COUNT(*) FROM attendence_info WHERE AttStatus='A' AND AttDate=Salman) AS Janu FROM attendence_info AS tab GROUP BY tab.AttDate"
);

2 个答案:

答案 0 :(得分:0)

在你形成查询的方式中访问值选项卡.AttDAte和attData = Salman彼此独立..就像是两个分开的查询..任何方式何时和如果值匹配可能是你有omogeneus行为。

答案 1 :(得分:0)

$sth = $conn->query(
    "SELECT tab.AttDate AS Salman,(SELECT COUNT(*) FROM attendence_info WHERE AttStatus='P' AND AttDate=Salman) AS Monu,
    (SELECT COUNT(*) FROM attendence_info WHERE AttStatus='A' AND AttDate=Salman) AS Janu FROM attendence_info AS tab GROUP BY tab.AttDate"
);

tab.AttDate的父查询和子查询的AttDate来自相同的attendence_info表。 tab.AttDate只是制作不同的别名,但两者都包含相同的列值.... 因此子查询可以使用tab.AttDate