开发者

How should this MySQL Query look like?

开发者 https://www.devze.com 2023-01-13 08:59 出处:网络
I have a table with the following design: IDOPT_1OPT_2OPT_3A_ID 13431 25211 31开发者_如何学Python221

I have a table with the following design:

ID    OPT_1    OPT_2    OPT_3    A_ID

1     3        4        3        1
2     5        2        1        1
3     1  开发者_如何学Python      2        2        1

I want to select all OPT's for A_ID 1, but when I run the query, I don't know the A_ID, I only know the OPT's.

So how can I insert a variable to get A_ID for the options I want? for instance:

SELECT * FROM table 
WHERE ((OPT_1 = 1 OR OPT_1 = 5) 
   AND (OPT_2 = 4 OR OPT_2 = 2) 
   AND (OPT_3 = 3 OR OPT_3 = 2) 
   AND A_ID = $X=$X)

Thanks,


Possibly best way to do it is to split it up a bit.

SELECT *
FROM table
WHERE A_ID in (
    SELECT A_ID
    FROM table 
    WHERE (OPT_1 = 1 OR OPT_1 = 5)
    union
    SELECT A_ID
    FROM table 
    WHERE (OPT_2 = 4 OR OPT_2 = 2)
)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号