MySQL Forums
Forum List  »  PHP

Re: Problem with SQL query :(
Posted by: Peter Brawley
Date: January 19, 2014 12:30PM

Simplifying your table & column names for human readability and less typing ...

+------+----------------+-------+----------+
| id   | id_marketplace | label | property |
+------+----------------+-------+----------+
|    1 |             10 |     3 |        0 |
|    2 |             10 |     6 |       35 |
|    4 |             10 |    10 |       22 |
|    5 |             10 |     9 |        0 |
|    6 |             11 |     3 |        0 |
|    7 |             11 |     6 |        5 |
|    8 |             11 |     7 |        7 |
|    9 |             11 |     7 |       10 |
|   10 |             11 |    10 |       21 |
|   11 |             12 |     3 |        0 |
|   12 |             12 |     6 |        5 |
|   13 |             12 |     7 |        8 |
|   14 |             12 |     7 |        9 |
|   15 |             12 |    10 |       21 |
|   16 |             13 |     3 |        0 |
|   17 |             13 |     6 |       35 |
|   18 |             13 |     7 |        7 |
|   19 |             13 |     7 |        8 |
|   20 |             13 |    10 |       20 |
+------+----------------+-------+----------+

Your second query condition ...

(`id_marketplace_properties_label` = 6 AND `id_marketplace_properties` = 35 ) AND
(`id_marketplace_properties_label` = 7 AND `id_marketplace_properties` = 7 )

would require that columns have two different values simultaneously---obviously not possible. So we need to clarify your requirement. You want the id_marketplace values for which ...

A there is a row where label=6 AND property=5)

OR

B there is a row where label=6 AND property=35, and another row where label=7 AND property=7.

The query for A is simple ...
SELECT id_marketplace 
FROM mpu
WHERE label=6 AND property=5;
+----------------+
| id_marketplace |
+----------------+
|             11 |
|             12 |
+----------------+

The query for B isn't so simple ...
SELECT a.id_marketplace 
FROM mpu a
JOIN mpu b 
  ON a.id_marketplace=b.id_marketplace 
 AND a.label=6 AND a.property=35 
 AND b.label=7 AND b.property=7;
+----------------+
| id_marketplace |
+----------------+
|             13 |
+----------------+

or mebbe a little more efficiently ...
SELECT id_marketplace
FROM mpu
WHERE (label=6 AND property=35) OR (label=7 AND property=7)
GROUP BY id_marketplace
HAVING COUNT(*) >=2;

You want id_marketplaces for condition A OR B; that's most efficiently got with a UNION ...

SELECT id_marketplace 
FROM mpu
WHERE label=6 AND property=5
UNION
SELECT a.id_marketplace 
FROM mpu a
JOIN mpu b ON a.id_marketplace=b.id_marketplace AND a.label=6 AND a.property=35 AND b.label=7 AND b.property=7;
+----------------+
| id_marketplace |
+----------------+
|             11 |
|             12 |
|             13 |
+----------------+



Edited 1 time(s). Last edit at 01/19/2014 12:40PM by Peter Brawley.

Options: ReplyQuote


Subject
Written By
Posted
January 18, 2014 10:39PM
Re: Problem with SQL query :(
January 19, 2014 12:30PM
January 19, 2014 12:41PM
January 20, 2014 03:24PM
January 21, 2014 06:22PM
January 22, 2014 04:19PM
January 23, 2014 12:15AM
January 23, 2014 10:23AM
January 19, 2014 12:33PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.