0

What SELECT query can i use to only show the cars WHERE bookings.status <> 1

so on the table below, cars.id (1, 3, 4, 6) will only show as the result

i'm stuck with this query or of this is any good:

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

EDIT: sorry i wasn't clear here, i also want the others listed as result even though they are not on the bookings table

7 Answers 7

1

Try this :

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

Try this if you didn't want records from Cars that are not in Bookings :

SELECT * FROM `Cars` as `C` Right JOIN `Booking` AS
 `B` ON `C`.`id` = `B`.`id`
Sign up to request clarification or add additional context in comments.

6 Comments

this will give me the opposite result that i wanted to have, it will return the cars.id(2,5)
means you want to get the result where status equal to 1 ?
sorry i wasn't clear here, i mean it will only show me the list of cars that are status <> 1, in this case only the cars.id (1), i also want the others listed as result even though they are not on the bookings table
show me in comments what result you expect.did you try left join ?
yes and same result, basically it will only show the cars that are listed on the bookings table. for example the car.id 6, will not show on the result because it has no bookings.car_id 6.
|
1

This will only show the cars

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

Comments

0

try this

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

Comments

0

Try This...

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

1 Comment

it will only show the cars that are listed on the bookings table. for example the car.id 6, will not show on the result because it has no bookings.car_id 6.
0

Assuming your car_id from your bookings table is the same column as the id in the cars table...

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

Comments

0

SOLVED IT with this query

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)

thanks everyone, it helped alot from your answers

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.