0

I have two tables, options and products. I want to select all from options but only if options.id exists in any of the products columns. This are my two tables:

options table:

id        option         value

1       'kategorija'    'Muški'
2       'kategorija'    'Ženski'
3       'kategorija'    'Dječji'
4       'brand'         'Casio'
5       'brand'         'Lorus'
6       'brand'         'Seiko'
7       'brand'         'Citizen'
8       'mehanizam'     'Quartz'
9       'mehanizam'     'Automatik'
10      'mehanizam'     'Eco-Drive'
11      'brojcanik'     'Analogni'
12      'brojcanik'     'Digitalni'
13      'grupa'         'Satovi'
14      'grupa'         'Naocale'

and the second table products:

 id     grupa   brand    mehanizam  brojcanik   kategorija  

10380   '13'     '4'        '8'        '11'         '2'
10560   '13'     '4'        '9'        '12'         '1'
11100   '13'     '6'        '8'        '11'         '2'
12380   '14'     '7'        '8'        '11'         '2'
12490   '13'     '6'        '9'        '11'         '1'
15720   '14'     '6'        '9'        '12'         '1'
16550   '14'     '5'        '8'        '12'         '3'

my attempt for query:

SELECT * FROM options WHERE EXISTS( SELECT 1 FROM products WHERE grupa="14" AND brand=options.id OR mehanizam=options.id OR brojcanik=options.id OR kategorija=options.id)

the result should be:

Array
(
    [0] => Array
        (
            [id] => 14
            [option] => grupa
            [value] => Naocale
        )

    [1] => Array
        (
            [id] => 7
            [option] => brand
            [value] => Citizen
        )

    [2] => Array
        (
            [id] => 8
            [option] => mehanizam
            [value] => Quartz
        )

    [3] => Array
        (
            [id] => 11
            [option] => brojcanik
            [value] => Analogni
        )

    [4] => Array
        (
            [id] => 2
            [option] => kategorija
            [value] => Zenski
        )
)

This is array only for one row for products id 12380 which is grupa 14. Array should go on for the rest of products where there is grupa 14 found in products row. I don't know if this is possible only with mysql query. If it's not possible i will have to do comparison with php which is what I'm trying to avoid.

Thanks

4

2 回答 2

3

根据你的说法,

我想从选项中选择所有选项,但前提是 options.id 存在于任何产品列中。

您可以在子查询中组合使用所有值UNION ALL并将结果与​​ table 连接options

SELECT  DISTINCT a.*
FROM    options a
        INNER JOIN
        (
            SELECT  brand col FROM products WHERE grupa = 14
            UNION ALL
            SELECT  mehanizam col FROM products WHERE grupa = 14
            UNION ALL
            SELECT  brojcanik col FROM products WHERE grupa = 14
            UNION ALL
            SELECT  kategorija col FROM products WHERE grupa = 14
        ) b ON a.id = b.col

更好的方法,

SELECT  * 
FROM    options a
WHERE   EXISTS
        ( 
            SELECT  1 
            FROM    products b
            WHERE   b.grupa = '14' AND 
                    a.ID IN (brand, mehanizam, brojcanik, kategorija)
        )
于 2013-09-23T14:35:23.423 回答
0

而不是专门使用14你可以简单地options再次加入

WHERE EXISTS (SELECT 1 FROM products JOIN options ON id = grupa ...
于 2013-09-23T14:35:07.270 回答