mysql select from table where two (or more) rows match,

Ivan Milanov

Let's say we have shirts in our database. All shirts have size and color (and many other options, but for now I'm trying to figure it out with two).

The table is something like:

+-------------------------+--------------+--------------+-----------+
| variation_attributes_id | variation_id | attribute_id | option_id |
+-------------------------+--------------+--------------+-----------+
|                       1 |            1 | size         | s         |
|                       2 |            1 | color        | red       |
|                       3 |            2 | size         | m         |
|                       4 |            2 | color        | red       |
|                       5 |            3 | size         | s         |
|                       6 |            3 | color        | green     |
|                       7 |            4 | size         | m         |
|                       8 |            4 | color        | green     |
+-------------------------+--------------+--------------+-----------+

How to select unique variation_id. For example where size is S and color is Green should return variation_id == 3.

Kleskowy

You could do that with a simple JOIN (self-join, meaning you join the table with itself under specific conditions). Assuming your table is named shirts, it goes like this:

SELECT DISTINCT s1.variation_id 
FROM shirts s1
JOIN shirts s2 ON s1.variation_id = s2.variation_id
WHERE (s1.attribute_id = 'size' AND s1.option_id='s')
  AND (s2.attribute_id = 'color' AND s2.option_id='green')

It will return all variation_id that have the color and size that you need, filtering out all other variation_id's.

This query will work fine and it's performance will be OK if you have and index like (variation_id, attribute_id, option_id)

What is more, this query lets you get variation_ids with more complicated arguments, which is much harder using HAVING COUNT(*) = ?.

For example - you need to find all variation_ids that have a size 's' or 'm', and color of 'green' or 'yellow'

SELECT DISTINCT s1.variation_id 
FROM shirts s1
JOIN shirts s2 ON s1.variation_id = s2.variation_id
WHERE (s1.attribute_id = 'size' AND s1.option_id IN ('s', 'm'))
  AND (s2.attribute_id = 'color' AND s2.option_id IN ('green', 'yellow'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql select from multiple tables where two items match

From Dev

How to select non-matching rows from two mysql table

From Dev

MySQL select row with two matching joined rows from another table

From Dev

MySQL Select all rows from table 1 and all rows from table2 where

From Dev

SQL Select rows from table where all joined rows match value

From Dev

Select rows from a pandas dataframe where two columns match list of pairs

From Dev

PHP/MySQL - Select all the rows where column from table 1 is equal to column from table 2

From Dev

PHP/MySQL - Select all the rows where column from table 1 is equal to column from table 2

From Dev

MySQL Select rows that match multiple rows in related table

From Dev

MySQL Select rows that match multiple rows in related table

From Dev

PHP pull from two MySQL tables, where multiple rows in table two

From Dev

Select rows from table where two colmns form a distinct set and order by third field

From Dev

MySQL - SELECT all from TABLE_1 where id does not exist in two fields of TABLE_2

From Dev

Select last two rows from mysql php

From Dev

How to select/match from MySQL table?

From Dev

Select rows from a table based on results from two other tables in mySQL using PDO

From Dev

mysql: select rows from another table as columns

From Dev

mysql - select from a table with sum() and where clause

From Dev

Select from mysql table WHERE field in '$array'?

From Dev

MySQL SELECT FROM 3 Table with where

From Java

sql two where clauses from one table into two new rows

From Dev

How to select all the SQL rows from a table (With a unique combination of two columns) where the same combination is not already in another table

From Dev

MySQL - Exclude all rows from one table if match on another table

From Dev

Table with two or more header rows

From Dev

How to select data from columns in only one table when checking two tables limited by where clause in MySQL

From Dev

How to select data from columns in only one table when checking two tables limited by where clause in MySQL

From Dev

MySQL Best select query from two table

From Dev

Select 2 or more rows in mysql

From Dev

Hive select from two tables where fields match?

Related Related

  1. 1

    mysql select from multiple tables where two items match

  2. 2

    How to select non-matching rows from two mysql table

  3. 3

    MySQL select row with two matching joined rows from another table

  4. 4

    MySQL Select all rows from table 1 and all rows from table2 where

  5. 5

    SQL Select rows from table where all joined rows match value

  6. 6

    Select rows from a pandas dataframe where two columns match list of pairs

  7. 7

    PHP/MySQL - Select all the rows where column from table 1 is equal to column from table 2

  8. 8

    PHP/MySQL - Select all the rows where column from table 1 is equal to column from table 2

  9. 9

    MySQL Select rows that match multiple rows in related table

  10. 10

    MySQL Select rows that match multiple rows in related table

  11. 11

    PHP pull from two MySQL tables, where multiple rows in table two

  12. 12

    Select rows from table where two colmns form a distinct set and order by third field

  13. 13

    MySQL - SELECT all from TABLE_1 where id does not exist in two fields of TABLE_2

  14. 14

    Select last two rows from mysql php

  15. 15

    How to select/match from MySQL table?

  16. 16

    Select rows from a table based on results from two other tables in mySQL using PDO

  17. 17

    mysql: select rows from another table as columns

  18. 18

    mysql - select from a table with sum() and where clause

  19. 19

    Select from mysql table WHERE field in '$array'?

  20. 20

    MySQL SELECT FROM 3 Table with where

  21. 21

    sql two where clauses from one table into two new rows

  22. 22

    How to select all the SQL rows from a table (With a unique combination of two columns) where the same combination is not already in another table

  23. 23

    MySQL - Exclude all rows from one table if match on another table

  24. 24

    Table with two or more header rows

  25. 25

    How to select data from columns in only one table when checking two tables limited by where clause in MySQL

  26. 26

    How to select data from columns in only one table when checking two tables limited by where clause in MySQL

  27. 27

    MySQL Best select query from two table

  28. 28

    Select 2 or more rows in mysql

  29. 29

    Hive select from two tables where fields match?

HotTag

Archive