我的数据库中有两个表tkt_booking和trip_assign。我需要使用第二个表(trip_assign)中的where子句来获取连接这两个表的数据。 这些是我的桌子
CREATE TABLE `tkt_booking` (
`id` int(11) UNSIGNED NOT NULL,
`id_no` varchar(20) DEFAULT NULL,
`trip_id_no` int(11) UNSIGNED DEFAULT NULL,
`tkt_passenger_id_no` varchar(20) DEFAULT NULL,
`trip_route_id` int(11) DEFAULT NULL,
`pickup_trip_location` varchar(50) DEFAULT NULL,
`drop_trip_location` varchar(50) DEFAULT NULL,
`price` float DEFAULT NULL,
`total_seat` int(11) DEFAULT NULL,
`seat_numbers` varchar(255) DEFAULT NULL,
`tkt_refund_id` int(11) DEFAULT NULL,
`agent_id` int(11) DEFAULT NULL,
`booking_date` datetime DEFAULT NULL,
`date` datetime DEFAULT NULL,
`booking_type` varchar(20) DEFAULT NULL,
`payment_status` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `trip_assign` (
`id` int(11) UNSIGNED NOT NULL,
`id_no` varchar(20) DEFAULT NULL,
`fleet_registration_id` int(11) DEFAULT NULL,
`trip_route_id` int(11) DEFAULT NULL,
`start_date` date DEFAULT NULL,
`end_date` time DEFAULT NULL,
`reporting` time DEFAULT NULL,
`sold_ticket` float NOT NULL DEFAULT '0',
`total_income` float DEFAULT '0',
`total_expense` float DEFAULT '0',
`total_fuel` float DEFAULT '0',
`trip_comment` text,
`closed_by_id` int(11) DEFAULT '0',
`date` datetime DEFAULT NULL,
`status` tinyint(1) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `tkt_booking`
ADD CONSTRAINT `FK_tkt_booking_trip_assign` FOREIGN KEY (`trip_id_no`) REFERENCES `trip_assign` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
从上面两个表中,我需要连接这些表并使用trip_assign.start_date
的where子句获取数据。
以下是我的codeigniter查询
public function read($data= array())
{
$start_date = date('Y-m-d', strtotime($data->start_date));
$end_date = date('Y-m-d', strtotime($data->end_date));
$this->db->select("
tb.*,
tr.name AS route_name,
ta.start_date AS start_date,
fr.reg_no AS bus_number,
a.agent_id AS agent_id,
tp.phone AS phone,
CONCAT_WS(' ', a.agent_first_name, a.agent_last_name) AS agent_name,
CONCAT_WS(' ', tp.firstname, tp.lastname) AS pass_name
")
->from("tkt_booking AS tb")
->join("trip_route AS tr", "tr.id = tb.trip_route_id", "left")
->join("trip_assign AS ta", "ta.id_no = tb.trip_id_no", "left")
->join("fleet_registration AS fr", "fr.id = ta.fleet_registration_id", "left")
->join("tkt_passenger AS tp", "tp.id_no = tb.tkt_passenger_id_no", "left")
->join("agent AS a", "a.agent_id = tb.agent_id", "left")
->group_start()
->where("tb.tkt_refund_id IS NULL", null, false)
->or_where("tb.tkt_refund_id", 0)
->or_where("tb.tkt_refund_id", null)
->group_end();
switch ($data->filter)
{
case 'trip':
$this->db->where('tb.trip_id_no', $data->trip);
break;
case 'bus':
$this->db->where('fr.reg_no', $data->bus);
break;
case 'agent':
$this->db->where('a.agent_first_name', $data->agent);
break;
}
$this->db->where("DATE(ta.start_date) BETWEEN '$start_date' AND '$end_date'", null, false);
return $this->db->limit($data->limit, $data->offset)
->order_by('ta.start_date', 'desc')
->get()
->result();
}
从上面的查询中,当我在where子句中使用ta.start_date时,我没有得到任何结果,但是我仅通过使用tb.date得到了结果。 我需要一些帮助