也许我累了,但有人能解释一下为什么我没有得到空的行吗?
我想要这个
230901
id from join
id from join
id from join
230902
no rows matches in table B
230903
id from join
id from join
id from join
id from join
id from join
230904
no rows matches in table B
现在它给了我:
230901
id from join
id from join
id from join
230903
id from join
id from join
id from join
id from join
id from join
表A确实有所有的日期,表B只有一些日期,我不明白怎么做才能让日期呼应,即使B没有那个日期。
$query = "SELECT B.B_id, B.B_time, B.B_date, B.B_start, B.B_end,
A.A_date, A.A_id
FROM A
LEFT JOIN B ON DATE(A.A_date) = B.B_date
WHERE A.A_id = $use_id AND A.A_date between '$first' and '$last' AND B.B_id = $use_id ";
$result = $db->query($query);
$appointments = array();
while ($row = mysqli_fetch_assoc($result))
{
if (!isset($appointments[$row['A_date']]))
{
$appointments[$row['A_date']] = array();
}
$appointments[$row['A_date']] []= $row;
}
foreach ($appointments as $date => $items)
{
echo "<h1>{$date}</h1>";
echo "<ul>";
foreach ($items as $item)
{
echo "<li>{$item['B_date']} {$item['B_id']} {$item['B_start']}:{$item['B_end']} " .
"({$item['B_time']})</li>";
}
echo "</ul>";
}
1条答案
按热度按时间shyt4zoc1#
最后一个AND语句这就是问题所在。连接导致A中的记录不包括在B中,然后被排除,因为B.B_ID将为null,这将永远不会匹配user_id,因为无法将其与字符串进行比较,因此您希望由左连接保留的那些记录现在被排除。
相反,将其移动到联接,否则左联接将被否定,其行为类似于内部联接。