两个表上的mysql查询

时间:2013-12-04 04:58:17

标签: php mysql

我可以使用哪种SELECT查询仅显示cars WHERE bookingsstatus<> 1

所以在下表中,carsid(1,3,4,6)只会显示为结果

我坚持这个问题,或者这是好事:

SELECT * FROM `cars` as `C` INNER JOIN `bookings` AS `B` ON `C`.`id` = `B`.`id` ....?

cars

  id | name
  -- | -------------
  1  | Car 1
  2  | Car 2
  3  | Car 3
  4  | Car 4
  5  | Car 5
  6  | Car 6

bookings

id | car_id | status
-- | ------ | ------
1  | 1      | 0
2  | 2      | 1
3  | 2      | 2
4  | 1      | 0
5  | 5      | 1
编辑:对不起我在这里不清楚,即使他们不在bookings

,我也希望其他人列出结果

7 个答案:

答案 0 :(得分:1)

试试这个:

SELECT * FROM `cars` as `C` INNER JOIN `bookings` AS
 `B` ON `C`.`id` = `B`.`id` where `B`.`status` <> 1

如果您不想要来自不在预订中的汽车的记录,请尝试此操作:

SELECT * FROM `Cars` as `C` Right JOIN `Booking` AS
 `B` ON `C`.`id` = `B`.`id`

答案 1 :(得分:1)

这只会显示汽车

SELECT c.name FROM `cars` as `C` INNER JOIN `bookings` AS
     `B` ON `C`.`id` = `B`.`id` where `B`.`status` != 1

答案 2 :(得分:0)

试试这个

SELECT * FROM `cars` C, `bookings` B WHERE C.id=B.car_id AND B.status!=1

答案 3 :(得分:0)

试试这个...

SELECT * FROM cars C LEFT JOIN bookings B ON C.id = B.car_id  WHERE B.status <> 1

答案 4 :(得分:0)

假设car_id表中的bookingsid表格中cars的列相同...

SELECT A.* from cars A
inner join bookings B on A.id=B.car_id
where b.status<>1;

答案 5 :(得分:0)

使用此查询解决了此问题

SELECT * FROM `cars` WHERE `id` NOT IN  (SELECT `B`.`car_id` FROM `cars` AS `C` INNER JOIN `bookings` AS `B` ON `C`.`id` = `B`.`car_id` WHERE `B`.`status` = 1)

感谢大家,它帮助了你的答案

答案 6 :(得分:0)

SELECT A.* , B.* FROM cars A, bookings B WHERE B.car_id=A.id AND B.status <> 1