Jump to content

Recommended Posts

Hi, I have columns

 

First name | last name | nr (individual number) | pref1 (preference1) | pref2 (preference2)| pref3(preference3) | situation | distance | sex

 

 

with 100 records in one table ap

 

 SELECT DISTINCT nr FROM ap 

 

First I have to display all records:

 

WHERE sex='F' and pref1='1' ORDER BY situation DESC, distance DESC
AND  WHERE (sex='F' and pref2='1' and situation= ' ' ) ORDER BY distance DESC
and WHERE (sex='F' and pref3='1' and situation= ' ' ) ORDER BY distance DESC
LIMIT 4

 

Then I would like to join results from:

WHERE sex='M' and pref1='1' ORDER BY situation DESC, distance DESC
AND WHERE (sex='M' and pref2='1' and situation= ' ' ) ORDER BY distance DESC
AND WHERE (sex='M' and pref3='1' and situation= ' ' ) ORDER BY distance DESC
LIMIT 7

 

and then join to all records from:

 

WHERE sex='F' and pref1='2' ORDER BY situation DESC, distance DESC
AND WHERE (sex='F' and pref2='2' and situation= ' ' ) ORDER BY distance DESC
AND WHERE (sex='F' and pref3='2' and situation= ' ' ) ORDER BY distance DESC
LIMIT 10

 

and so on...

 

 

In the results of all I can't have redundancies. It means when in first group of result I get individual number (column "nr") of '2112' it can't be displayed in last one of results.

 

Is it possible to do?

Link to comment
https://forums.phpfreaks.com/topic/265347-how-to-join-3-queries/
Share on other sites

If I understand you correctly, what you can do is this:

 

SELECT * FROM
  ( SELECT ... )
UNION ( SELECT ... )
UNION ( SELECT ... )

 

This will give you the unique results from all of the subqueries.

 

It depends what you mean by "join" though - usually a join in SQL means to match up results based on common data, such as "same name" or "same id number", and put the matching data in the same result row.  Your question sounds like you want to combine the result sets using "union", which basically means "Get these results, and these results, and these results, and return them all".

  • 1 year later...
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.