Get rows with matching values in column in same table

Grogina

I have the following DDLs...

CREATE TABLE IF NOT EXISTS `product` (
  `id_product` int(10),
  `id_manufacturer` int(10)
  );


INSERT INTO `product` (`id_product`, `id_manufacturer`) VALUES
(1,1),
(2,1),
(3,2),
(4,1),
(5,2);

CREATE TABLE IF NOT EXISTS `feature_product` (
  `id_feature` int(10),
  `id_product` int(10),
  `id_feature_value` int(10)
);


INSERT INTO `feature_product` (`id_feature`, `id_product`, `id_feature_value`) VALUES
(5, 1, 9),
(5, 2, 9),
(5, 3, 10),
(5, 4, 10),
(7, 5, 10);

http://sqlfiddle.com/#!2/cbe05/1/0

Can you explain me please, how I can get - all Products with the same Manufacturer and the same Feature_value?

Now (in project) I do it with 2 additional SELECT's (for getting id_manufacturer and id_feature_value), but maybe there are more correct (and fast) way?

Thanks for your time and sorry for my English)

I need too see result like this:

id_product | 
-----------|
1          |
2          |

only this 2 products have same manufacturer and (at the same time) same feature value

Neels

Just use GROUP_CONCAT:

SELECT GROUP_CONCAT(p.id_product SEPARATOR '\n') AS Products FROM product p INNER JOIN feature_product fp ON (p.id_product = fp.id_product AND fp.id_feature = 5) GROUP BY p.id_manufacturer, fp.id_feature_value HAVING COUNT(p.id_manufacturer) > 1 AND COUNT(fp.id_feature_value)>1;

This will give you the list of Products having multiple Manufacturer Id and Feature Value in a single line, separated by a newline character. You can change the separator as your requirement.

Here is the SQL Fiddle link:

http://sqlfiddle.com/#!2/cbe05/70

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Server compare values of two rows of same table and get not matching column names

From Dev

Combine rows in same column and table with similar values and sum values

From Dev

Formula to check if rows that have the same value in one column have matching values in another column

From Dev

Get specific values from same column within grouped rows

From Dev

How to get rows from multiple values of the same column

From Dev

get all rows from table that have both variables in a same column

From Dev

SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows

From Dev

Iterating over all rows with the same id and find matching values in a different table

From Dev

How to get data by using different column values refrencing same table

From Dev

How to get data by using different column values refrencing same table

From Dev

How to get column name from table from 2 rows of same table having same value?

From Dev

How to get column name from table from 2 rows of same table having same value?

From Dev

How to set the column values of some rows the same as the some other ones in the same table?

From Dev

Inserting multiple rows into a table with the same foreign key in column1 & different values for column2

From Dev

SQL - Get value of column for rows matching a string AND a list of strings not found in the table?

From Dev

LEFT JOIN table to find non matching rows, same table

From Dev

Comapare values in same column same table

From Dev

data.table in R: Replace a column value with a value from same column after matching two other columns values

From Dev

Copy rows from and within same oracle SQL table and changing some column values

From Java

PostgreSQL select rows having same column values

From Dev

Select rows with same column values (Mysql)

From Dev

find a continuous range of rows with the same column values

From Dev

find a continuous range of rows with the same column values

From Dev

merge 2 rows based on the same column values

From Dev

Subsetting rows with range of column having same values

From Dev

Excel: how to sort rows by same column values

From Dev

fetch rows data with same values in two column

From Dev

SQL get multiple values (from the same column) from same table using multiple queries

From Dev

How to get data from table with no matching column?

Related Related

  1. 1

    SQL Server compare values of two rows of same table and get not matching column names

  2. 2

    Combine rows in same column and table with similar values and sum values

  3. 3

    Formula to check if rows that have the same value in one column have matching values in another column

  4. 4

    Get specific values from same column within grouped rows

  5. 5

    How to get rows from multiple values of the same column

  6. 6

    get all rows from table that have both variables in a same column

  7. 7

    SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows

  8. 8

    Iterating over all rows with the same id and find matching values in a different table

  9. 9

    How to get data by using different column values refrencing same table

  10. 10

    How to get data by using different column values refrencing same table

  11. 11

    How to get column name from table from 2 rows of same table having same value?

  12. 12

    How to get column name from table from 2 rows of same table having same value?

  13. 13

    How to set the column values of some rows the same as the some other ones in the same table?

  14. 14

    Inserting multiple rows into a table with the same foreign key in column1 & different values for column2

  15. 15

    SQL - Get value of column for rows matching a string AND a list of strings not found in the table?

  16. 16

    LEFT JOIN table to find non matching rows, same table

  17. 17

    Comapare values in same column same table

  18. 18

    data.table in R: Replace a column value with a value from same column after matching two other columns values

  19. 19

    Copy rows from and within same oracle SQL table and changing some column values

  20. 20

    PostgreSQL select rows having same column values

  21. 21

    Select rows with same column values (Mysql)

  22. 22

    find a continuous range of rows with the same column values

  23. 23

    find a continuous range of rows with the same column values

  24. 24

    merge 2 rows based on the same column values

  25. 25

    Subsetting rows with range of column having same values

  26. 26

    Excel: how to sort rows by same column values

  27. 27

    fetch rows data with same values in two column

  28. 28

    SQL get multiple values (from the same column) from same table using multiple queries

  29. 29

    How to get data from table with no matching column?

HotTag

Archive