0

I am using SQL for the first time and can't seem to figure out how can I convert this query into a join. I want to do so, because I read this: Join vs. sub-query

SELECT `bookings`.* FROM `bookings` WHERE `bookings`.`user_id` IN 
(SELECT `users`.`id` FROM `users` WHERE `users`.`phone` = 9999999999)

I want to find only those bookings whose users belong in the user table with a given phone number.

I tried using a join, but I don't understand what the possible condition of join should be.

Thanks a lot!

Community
  • 1
  • 1

2 Answers2

1

You need something like that:

    SELECT b.* FROM `bookings` b 
       INNER JOIN `users` u 
       ON b.user_id = u.id
    WHERE u.phone = 8860990440
Thanos Markou
  • 2,587
  • 3
  • 25
  • 32
-1
SELECT `bookings`.* FROM `bookings` INNER JOIN `users` ON `users`.`id` = `bookings`.`user_id` WHERE `users`.`phone` = 9999999999
raven
  • 2,574
  • 2
  • 27
  • 49