开发者

How should this MySQL Query look like?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜