d3bug3r
d3bug3r

Reputation: 2586

mysql select related items based on current item

i have a scenario, lets say 3 users review a business of id 10, how can i get all the unique id of the user who review that business and and use that unique id to find another business review which is not equal to 10 ?

sample table user_review:

review_id | user_id | business_id | rating | review_date
  1           2          10           3       20121030124001
  2           2          9            3       20121022120627
  3           2          10           4       20121023120627
  4           3          10           4       20121024120627
  5           3          6            3       20121022140627
  6           4          10           2       20121025120627
  7           4          10           5       20121030120627
  8           3          10           2       20121010120627
  9           4          8            5       20121028120627

i should get result of these

review_id | user_id | business_id | rating | review_date
  2           2          9            3       20121022120627
  5           3          6            3       20121022140627
  9           4          8            5       20121028120627

In a above result if there is 2 reviews for a same user_id and same business_id the latest one should be return.Thanks

Upvotes: 1

Views: 842

Answers (3)

Farfarak
Farfarak

Reputation: 1527

Try this query:

Here is link to sqlfidle with running results http://sqlfiddle.com/#!2/cd4ea/1

SELECT
  tblreview.*,tblusers.user_name
FROM
  (
      SELECT
           MAX(tblreview.review_id) review_id
           , tblreview.user_id
      FROM
          (
            SELECT
                DISTINCT user_id
            FROM
               tblreview
            WHERE business_id = 10
          ) reviewsb10
      INNER JOIN
           tblreview
      ON
           tblreview.user_id = reviewsb10.user_id
      AND
           tblreview.business_id <> 10
      GROUP BY
           user_id
  ) tblLastReviewPerUser
INNER JOIN
  tblreview
ON
  tblreview.review_id = tblLastReviewPerUser.review_id
INNER JOIN
  tblusers
ON
  tblusers.user_id = tblLastReviewPerUser.user_id

Upvotes: 2

Devart
Devart

Reputation: 122032

Try this query -

SELECT * FROM (
    SELECT * FROM user_review
    ORDER BY IF(business_id = 10, 1, 0), review_date DESC
  ) t
GROUP BY user_id
HAVING
  COUNT(IF(business_id = 10, 1, NULL)) > 0
  AND COUNT(IF(business_id <> 10, 1, NULL)) > 0

+-----------+---------+-------------+--------+----------------+
| review_id | user_id | business_id | rating | review_date    |
+-----------+---------+-------------+--------+----------------+
|         2 |       2 |           9 |      3 | 20121022120627 |
|         5 |       3 |           6 |      3 | 20121022140627 |
|         9 |       4 |           8 |      5 | 20121028120627 |
+-----------+---------+-------------+--------+----------------+

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79979

SELECT t1.*
FROM TableName t1
INNER JOIN 
(
    SELECT user_id, business_id, MAX(review_date) review_date
    FROM TableName
    WHERE business_id <> 10
    GROUP BY user_id, business_id
) t2 ON  t1.user_id = t2.user_id 
     AND t1.review_date = t2.review_date
     AND t1.business_id = t2.business_id

Upvotes: 0

Related Questions