Mysql get records having multiple values on a different (relational) table (manyToMany)
NickName:Canser Yanbakan Ask DateTime:2017-11-20T17:46:24

Mysql get records having multiple values on a different (relational) table (manyToMany)

I need some help getting records based on a criteria where joining items table with their values table, having multiple search values.

Items table

+----+-----------+
| id |   title   |
+----+-----------+
| 82 | Test ITEM |
+----+-----------+

Attributes table

+----+--------------+
| id |  Attribute   |
+----+--------------+
|  1 | An Attribute |
+----+--------------+

Item Attribute Values table

+----+---------+--------------+-----------+
| id | item_id | attribute_id | option_id |
+----+---------+--------------+-----------+
|  1 |      82 |            1 |        25 |
|  1 |      82 |            1 |         3 |
+----+---------+--------------+-----------+

What i did:

SELECT 
p.*
FROM 
product_products p 
LEFT JOIN
product_attribute_values v ON v.product_id = p.id
LEFT JOIN
product_attribute_options o ON o.id = v.option_id
WHERE 
(p.id = 82 OR p.parent_id = 82) AND
(o.id IN (25, 3))
GROUP BY p.id

But there is a problem with this query because it returns all items having 3 OR 25 values.

I want to return items only that has 25 and 3 values at the same time.

Tried this: Advanced Mysql Query to get master record if two conditions matches on different rows of child records

Same result.

Please help.

Copyright Notice:Content Author:「Canser Yanbakan」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47389039/mysql-get-records-having-multiple-values-on-a-different-relational-table-many

More about “Mysql get records having multiple values on a different (relational) table (manyToMany)” related questions

Mysql get records having multiple values on a different (relational) table (manyToMany)

I need some help getting records based on a criteria where joining items table with their values table, having multiple search values. Items table +----+-----------+ | id | title | +----+----...

Show Detail

SQL records with multiple values for a field

What is the best way to design your table schemas for records that may have multiple values for a certain field? For example, a document can have multiple concepts associated with it. Each docume...

Show Detail

SWIFT - Coredata Delete relational records

I'm using core data with table A having one to many with table B. Trying to delete records from table B which are related to record in table A. When I was trying to delete, it only removes the rela...

Show Detail

Compare Records and display the column name which are having different values

Here is the sample table what I have. I have multiple err# one among is Err# 2 Err# ItemNo Price Company Model 2 101 50 ABC A12 2 101 50 ABC A12 2...

Show Detail

Get the relational data of multiple records

Is it possible to get the relational data of multiple rows at a time Like I have table countries and cities table and in cities it has relation with country (some has and some does not have) So if ...

Show Detail

Blending Relational with None-Relational (MySQL and MongoDB)

This has been talked about before but I have yet to come across a clear answer, its just roughly described as fire and water and left at that (from my research). Relational and None Relational dat...

Show Detail

MYSQL combine view of two table having different number of entry records in date range

Referencing to my previous question, which I asked some days ago and got the solution. and answer was pretty nice.. but now the problem with that is when I am giving range to my table records.. it ...

Show Detail

Mysql - Update multiple records with different values on every record

Table1 have 5 records or more with different 'products' and different 'price' and 'discount' and 'NetPrice' for every product, my code below updates all the records at once but with one value which...

Show Detail

Symfony2 Doctrine Count ManyToMany Non-Related Records

Here is the short story: I have 3 tables (1 is just for manytomany relation), that's news and clubs. News can be related to clubs with manytomany. It also can be no relational. For example; i have

Show Detail

how to import excel data to mysql relational table?

I have developed project in laravel for accounting related activity which has many relational tables with foreign keys in mysql. Everything works fine but the problem is my client has an excel file...

Show Detail