0

I have three tables such as "tbl_purchase", "tbl_user" & "tbl_contestant". I need all records from tbl_purchase with "name" that has in two tables "tbl_user" & "tbl_contestant".

My Query,

    SELECT tp.*, tu.* 
       FROM tbl_purchase tp
       INNER JOIN tbl_user tu 
       ON tu.user_id = tp.user_id               

Table: tbl_purchase

 +-------+-------------+------+
 | p_id  | user_id     | Star |
 +-------+-------------+------+
 | 1     | 1           | 50   |
 | 2     | 4           | 100  |
 | 3     | 6           | 150  |
 +-------+-------------+------+

Table: tbl_user

 +-------+-------------+
 | u_id  | name        |
 +-------+-------------+
 | 1     | Sachin      |
 | 4     | Akshay      |
 +-------+-------------+

Table: tbl_contestant

 +-------+-------+-------------+
 | c_id  | u_id  |  Name       |
 +-------+-------+-------------+
 | 1     |   6   |  Mayank     |
 | 2     |   8   |  Nikhil     |
 | 3     |   9   |  Vipul      |
 +-------+-------+-------------+

I want below result,

 +-------+-------+-------------+------+
 | p_id  | u_id  |  Name       | Star |
 +-------+-------+-------------+------+
 | 1     | 1     |  Sachin     | 50   |
 | 2     | 4     |  Akshay     | 100  |
 | 3     | 6     |  Mayank     | 150  |
 +-------+-------+-------------+------+
5
  • Are you using model ? Commented Mar 19, 2020 at 5:35
  • No, I have not used model Commented Mar 19, 2020 at 5:36
  • @ManojGhediya can you add queries that you tried so far? If you can add some queries you tried, someone can guide you appropriately. Commented Mar 19, 2020 at 5:38
  • LEFT JOIN both slave tables and use COALESCE() for to find the name. Decide what name must be returned if the records from slave tables contains the same user id with different names. Commented Mar 19, 2020 at 5:41
  • 1
    @ManojGhediya you have asked a question on stackoverflow before also and have not accepted an answer for the previous question. If you find answers helpful, please go ahead and mark one of the response as an answer so as to put closure to your question. I'd recommend reviewing the previous question you asked and marking that as answered if that answer helped you. Commented Mar 19, 2020 at 6:00

2 Answers 2

1

Try this SQL query:

select p.p_id, u.u_id, u.name, p.star
from tbl_purchase p
inner join (
    select u_id, name from tbl_user
    union
    select u_id, name from tbl_contestant
) u on p.user_id = u.u_id;

Result:

p_id    u_id    name    star
1       1      Sachin   50
2       4      Akshay   100
3       6      Mayank   150

Explanation:

  • Your users are in 2 tables - tbl_user and tbl_contestant
  • So, you have to combine the users using a UNION (which removes duplicates)
  • Then, you can join with your purchase table

Example: https://rextester.com/RKHXL43587

Sign up to request clarification or add additional context in comments.

Comments

0

Try this. Because of your name is not in a single table so leftJoin on both user and contestant table. and as @Akina said in comment COALESCE returns first non-null value from the list Reference

select tbl_purchase.p_id, 
tbl_purchase.user_id as u_id, 
COALESCE(tbl_user.name, tbl_contestant.Name) as name 
        from tbl_purchase 
        LEFT JOIN tbl_user ON tbl_user.u_id = tbl_purchase.user_id 
        LEFT JOIN tbl_contestant ON tbl_contestant.u_id = tbl_purchase.user_id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.