Friday, 15 May 2015

mysql - Writing a SQL query that gets information from 3 different tables -


I could not understand how to write these two questions on the tables I created. Trying

  Find users who review both of those stores and restaurants. Find users who review those businesses, but not shops or restaurants.  

The tables I am using

  Reviews; + ------------- + --------- + ------ + ----- + --------- + - - ---- + | Field | Type | Faucet Key | Default | Extra | + ------------- + --------- + ------ + ----- + --------- + - - ---- + | Business_id | Int (11) | No | PRI | Faucet | | User_id | Int (11) | No | PRI | Faucet | | Review_id | Int (11) | No | PRI | Faucet | | Review_date | Date | Yes. | Faucet | | Star_rating | Int (1) | Yes. | 1 | Business + -------------- + -------------- + ------ + ----- + ----- ---- + ------- + | Field | Type | Faucet Key | Default | Extra | + -------------- + -------------- + ------ + ----- + ------ --- + ------- + | Business_id | Int (11) | No | PRI | Faucet | | Name | Varchar (50) | Yes. | Faucet | | City | Varchar (40) | Yes. | Faucet | | State | Varchar (20) | Yes. | Faucet | | Full_address | Varchar (120) | Yes. | Faucet | Users; + ------------ + ------------- + ------ + ----- + --------- + ------- + | Field | Type | Faucet Key | Default | Extra | + ------------ + ------------- + ------ + ----- + --------- + ------- + | User_id | Int (11) | No | PRI | Faucet | | Name | Varchar (50) | Yes. | Faucet | | User_since | Date | Yes. | Explain NULL is_a_restaurant; + -------------- + ------------- + ------ + ----- + ------- - + ------- + | Field | Type | Faucet Key | Default | Extra | + -------------- + ------------- + ------ + ----- + ------- - + ------- + | Business_id | Int (11) | No | PRI | Faucet | | Type of food. Varchar (20) | Yes. | Faucet | | Total Sets | Int (11) | Yes. | 1 | | + -------------- + ------------- + ------ + ----- + ------- - + ------- + is_a_shop Explain; + ------------- + ------------- + ------ + ----- + -------- - + ------- + | Field | Type | Faucet Key | Default | Extra | + ------------- + ------------- + ------ + ----- + -------- - + ------- + | Business_id | Int (11) | No | PRI | Faucet | | Store_type | Varchar (50) | Yes. | Faucet |  

I honestly do not know where to start it. I know that I am going to join businesses with Business ID, but I do not know how I am going to find people whose shops and restaurants are reviewed. Can anyone help?

Edit: What I've tried

For the first query: Find users who review both shops and restaurants.

  SELECT B.business_id FROM is_a_shop b JOIN review on r.business_id = b.business_id JOIN is_a_restaurant k at r.business_id = k.business_id;  

This is the first one that should appear first: you can change the selection statement based on What information do you want to see?

  Select You Wherever you are present (SELECT * FROM is_a_shop s WHERE s.business_id = rev .business_id) or existing (SELECT * FROM is_a_restaurant r WHERE r.business_id = rev.) Where the user u inner join review rev revus.user_id = u.user_ID Business_id) It basically pulls all the users who wrote the reviews, where business_id is found in the table or is_a_restaurant. You can easily understand the second query. 


No comments:

Post a Comment