Jump to content

Recommended Posts

Hey guys,

 

I want to creat a SQL-Query and I don't know how to construct the part of "where".

 

There are four tables

 

1: users - important column ID (USER_ID)

2: profiles (each user is able to create more than one profile) - important column WORLD_ID, USER_ID

(3: comp_participants (each user is able to create more than one entry in the table, each comp_participant is connected to "comp"))

4: comp - important column WORLDS (e.g. "2, 4, 20")

 

$user_id is set (e.g. 44)

 

Now I want to show all entries of "comp" if the user got a profile on a world which is listed in WORLDS in the table "comp". I thing it is a very difficult kind of SQL-query and I look forward to a answer ;-)

 

Best regards ;-)

 

Link to comment
https://forums.phpfreaks.com/topic/243231-sql-query-with-4-tables/
Share on other sites

Hey Marcel,

 

try below query

 

SELECT c.* FROM users u 
LEFT JOIN profiles p ON p.USER_ID = u.USER_ID 
LEFT JOIN comp c ON c.WORLD_ID = p.WORLD_ID
LEFT JOIN comp_participants cp ON cp.COMP_ID = c.COMP_ID
WHERE u.USER_ID = 44

 

Let me know the result of the query.

Hey ;-)

 

thank you for creating the query!

 

I tried it but there is a malfunction in the script. It shows as many results as available profiles, in reference to the USED_ID. If the user got 3 profiles, the result contains 3 rows, independent whether the WORLD_ID matches the column "WORLDS" in "comp". The rows that are not matching with "comp" are empty, the right ones are filled with the correct content.

 

Thanks ;-)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.