mysql连接显示错误的PHP结果
我有以下三个表格: 出租车:mysql连接显示错误的PHP结果,php,mysql,Php,Mysql,我有以下三个表格: 出租车: id shopid taxiId status 1 20 1 1 2 20 2 1 3 20 3 2 4 20 4 1 5
id shopid taxiId status
1 20 1 1
2 20 2 1
3 20 3 2
4 20 4 1
5 21 1 1
...
id shopid taxiId status
1 20 1 booked
2 20 3 booked
id shopid taxiId status
1 20 2 booked
2 20 4 booked
预预订:
id shopid taxiId status
1 20 1 1
2 20 2 1
3 20 3 2
4 20 4 1
5 21 1 1
...
id shopid taxiId status
1 20 1 booked
2 20 3 booked
id shopid taxiId status
1 20 2 booked
2 20 4 booked
usrBooking:
id shopid taxiId status
1 20 1 1
2 20 2 1
3 20 3 2
4 20 4 1
5 21 1 1
...
id shopid taxiId status
1 20 1 booked
2 20 3 booked
id shopid taxiId status
1 20 2 booked
2 20 4 booked
现在我想从taxi
表(例如shop id=20)中获取所有记录,以及preBooking
和usrBooking
表的匹配记录
例如,我希望获得这样的记录(如果我在参数中传递了shop id 20)
我尝试了以下代码,但没有正常工作,显示了错误的结果
$shopid="20";
$this->db->select('t.taxiId,ub.status as usrBookingstatus ,pb.status as preBookingstatus ');
$this->db->from('taxi t');
$this->db->join('usrBooking ub', 'ub.taxiId=t.taxiId','LEFT OUTER');
$this->db->join('preBooking pb', 'pb.taxiId=t.taxiId','LEFT OUTER');
$this->db->where('t.shopid', $shopid);
$this->db->order_by('t.taxiId', 'ASC');
您的select语句似乎缺少几列。改变
$this->db->select('t.taxiId,ub.status as usrBookingstatus ,pb.status as preBookingstatus ');
到
首先,我不确定这是什么:
$this->db->where('c.salon_id', $shopid);
查询的FROM中没有表“c”,上面的示例数据中没有提到“salon_id”。这使查询无效,您无法运行它。也许这就是问题所在。可能您应该将其更改为:
$this->db->where('t.shopid', $shopid);
此外,在“选择”对话框中缺少一些列。完整查询应为:
SELECT t.id, t.shopid, t.taxiId, pb.status as preBookingstatus, ub.status as usrBookingstatus
FROM taxi t
LEFT JOIN usrBooking ub ON ub.taxiId=t.taxiId
LEFT JOIN preBooking pb ON pb.taxiId=t.taxiId
WHERE t.shopid = 20
ORDER BY t.taxiId ASC;
或者使用您的PHP代码:
$shopid="20";
$this->db->select('t.id, t.shopid, t.taxiId, pb.status as preBookingstatus, ub.status as usrBookingstatus');
$this->db->from('taxi t');
$this->db->join('usrBooking ub', 'ub.taxiId=t.taxiId','LEFT OUTER');
$this->db->join('preBooking pb', 'pb.taxiId=t.taxiId','LEFT OUTER');
$this->db->where('t.shopid', $shopid);
$this->db->order_by('t.taxiId', 'ASC');
你能分享从当前查询中收到的结果吗?因为“发件人”中仍然没有表“c”。应该是“t.shopid”,所以。。。还有问题吗?