2

here is my setup.

  • events: date, time, user_id, business_id
  • businesses: id, timezone, name, etc...
  • users: id, email, name, etc...

I want my select statement WHERE to look something like this:

events.date = $myDate, use events.business_id to get timezone from businesses.id and timezone = $myTimezone, and get all info from users where events.user_id = users.id

SELECT * FROM events, businesses, users WHERE ...

Is it possible to do this in one select query? If so, how? Thanks

Alireza
  • 100,211
  • 27
  • 269
  • 172
pixie123
  • 929
  • 3
  • 14
  • 27
  • Exactly how you might think: `WHERE events.date = '.$myDate.' ...` – colburton Jul 08 '17 at 18:12
  • 1
    Maybe this link will help [https://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables](https://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables) – Van Tho Jul 08 '17 at 18:14
  • 1
    Don't use the comma separated joins, use the explicit `join` syntax with `on`s. You can filter down with a `where` and the table aliases as you make them. For example, `from events as e join buisnesses as b on e.business_id = b.ud where b.name = 'chris cool company' and u.name = 'chris85'`.. i didnt define the `u` here but you get the picture? or you could use an `or` if you wanted my cool company, and all my other businesses. – chris85 Jul 08 '17 at 18:21

1 Answers1

2
SELECT * 
FROM events E
     INNER JOIN businesses B ON E.business_id = B.id
     INNER JOIN USERS U ON E.user_id= U.id
WHERE 
     E.date = $myDate