PPS
PPS

Reputation: 552

want to get the Unique Value MYSQL , ARRAY, PHP

We have a MySQL query, we used UNION ALL, We are getting two values for each table we just want to get only one single value, or we do one thing more, like we store the whole result into Array and then sort accordingly, then call the ID from array through Foreach loop

Here is the MYSQL query

  $num = mysql_query("(SELECT resorts.id From resorts,JUNE WHERE JUNE.id = resorts.id and JUNE.table_type = 'disp' AND JUNE.JUNE1!=0 AND resorts.category IN (1,2,3,4) AND resorts.area IN (1,2,3,4) Group By resorts.id LIMIT 0,7 ) UNION ALL (SELECT resorts.id From resorts,JULY WHERE JULY.id = resorts.id and JULY.table_type = 'disp' AND JULY.JULY1!=0 AND resorts.category IN (1,2,3,4) AND resorts.area IN (1,2,3,4) Group By resorts.id LIMIT 0,7 )");

We are getting the data like this ...

JUNE Table ID = 78 JUNE Table ID = 124 JUNE Table ID = 11 JULY Table ID = 78 JULY Table ID = 124

We just want to do this time, we only get the single value for any Double ID. like if we are getting JUNE ID = 78 and we again getting the same ID for JULY ID = 78 .We just want to do the Unique list of (Double) id's and ignore the Single repeated id's . Like this JUNE Table ID = 78 JUNE Table ID = 124

And ignore the rest of the id's,

NOTE :- our main motive is that, we only pass those id who are available in JUNE and JULY month, we ignore all the value if there are only in single months.

Upvotes: 0

Views: 490

Answers (2)

Abhay
Abhay

Reputation: 6645

I'm not sure of your table structure or data but for retrieving Resort IDs that are available both in JUNE and JULY, you can use INNER JOIN between the 3 tables, like:

SELECT resorts.id
FROM resorts INNER JOIN JUNE ON resorts.id = JUNE.id
INNER JOIN JULY ON resorts.id = JULY.id
...
...

OR, if the above is not what you are looking for and you want to avoid duplicate IDs, use UNION instead of UNION ALL

Upvotes: 0

dynamic
dynamic

Reputation: 48091

Don't use UNION ALL, use UNION

Or make all your query into a subquery like this:

SELECT DISTINCT * FROM ($yourOldQueryHere)

Upvotes: 0

Related Questions