Wednesday, 15 May 2013

mysql - Writing an sql query that uses count -


I am trying to write a query that uses SQL in the calculation. I am trying to write it The query is

  Find users who review at least 2 restaurants  

These are the tables I am using:

< Pre> explain_ar_rerestant; + -------------- + ------------- + ------ + ----- + ------- - + ------- + | 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 | | + -------------- + ------------- + ------ + ----- + ------- - + ------- + Review the review; + ------------- + --------- + ------ + ----- + --------- + - - ---- + | 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 | | + ------------- + --------- + ------ + ----- + --------- + - - ---- Explain the users; + ------------ + ------------- + ------ + ----- + --------- + ------- + | Field | Type | Faucet Key | Default | Extra | + ------------ + ------------- + ------ + ----- + --------- + ------- + | User_id | Int (11) | No | PRI | Faucet | | Name | Varchar (50) | Yes. | Faucet | | User_since | Date | Yes. | Faucet |

Any attempt made here (Ive tried a lot from this, but there is one):

  SELECT reviews.user_id from review JOIN is_a_restaurant ON (review .business_id = is_a_restaurant) WHERE (count (*). Is_a_restaurant & gt; 1) Reviews by Group .user_id ASC;  

is the error that I get

  There is an error in your SQL syntax; Close '.is_a_restaurant & gt; To use the correct syntax, check the manual related to your MySQL server version. 1) Reviews by Group .user_id ASC 'on Line 1  

I am not a MySQL My syntax may be a little bit of man, but you probably want to use a hosting clause.

Reviews. Reviews from user reviews at JOIN is_a_restaurant Business_id = is_a_restaurant.business_id GROUP by review .user_id ASC HAVING COUNT (*)> 1;

The Housing Clause is like a WHERE clause, but it is used for accumulated values ​​(COUNT in this case).

The column names have also disappeared from you_a_restaurant in JOIN expression.


No comments:

Post a Comment