PHP Query To Filter Many-To-Many MySQL

user1259798

I have the following query:

SELECT DISTINCT Names FROM all_uk
INNER JOIN cat_uk ON all_uk.Ref = cat_uk.Ref
INNER JOIN res_uk ON all_uk.Ref = res_uk.Ref
WHERE (cat_uk.Cat_Ref IN (25, 35)) AND (res_uk.Res_Ref = 1) AND (res_uk.Res_Ref = 2)

As you can see, the first part of the WHERE query is cumulative (everything that has either a 25 or a 35 ref). The second part is where I'm failing: I want to restrict the results to those where they have 1 AND a 2 in their second category.

Selecting one of these (i.e. either 1 OR 2) returns results, but when they're both selected I get nothing.

Can anyone help?

Mihai

I think you want this query

SELECT Names FROM all_uk
INNER JOIN cat_uk ON all_uk.Ref = cat_uk.Ref
INNER JOIN res_uk ON all_uk.Ref = res_uk.Ref
WHERE (cat_uk.Cat_Ref IN (25, 35)) 
GROUP BY Names
HAVING SUM(res_uk.Res_Ref = 1)>0
   AND SUM(res_uk.Res_Ref = 2)>0

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Django query filter many to many to many etc

分類Dev

MYSQL Many to many query to same table

分類Dev

mysql query - one to many relations

分類Dev

MySQL select with multiple many to many joins causing very slow query

分類Dev

backward filter and many to many relationship

分類Dev

mysql join query taking too many resources

分類Dev

query for many to many record matching

分類Dev

Many to many relation and query builder

分類Dev

Ef core 5 many to many filter

分類Dev

Database Query - One to Many

分類Dev

Query annotation count of list for many to many mapping

分類Dev

Django query in One to Many relationship

分類Dev

Django query in One to Many relationship

分類Dev

one query for many similar tables

分類Dev

SQL Server : how can I filter a many to many relationship with delimiter

分類Dev

How to filter a many2many field in odoo8?

分類Dev

Remove CROSS JOIN LATERAL from postgres query that spans many to many

分類Dev

using has_one, has_many, and many_many in controller/to filter queries

分類Dev

Upsert many documents in MongoDB and php

分類Dev

How many "AND" can you have on a SpringData query?

分類Dev

Joining two tables in mysql - One to many relationship

分類Dev

Automapper many to many mapping

分類Dev

SQL Many to Many JSON

分類Dev

Many to Many in two directions

分類Dev

SQL Many to Many Ranking

分類Dev

Many-to-Many in MongoDB

分類Dev

Unset many session from application in php but not all

分類Dev

Count how many children are in XML with PHP

分類Dev

EF Core How to filter a nested many-to-many list by another list