How do I query this table if I wanna select rows with same CHANGE_KEY and SYSTEM_KEY = 1 and SERVER_KEY = 2, to clarify I'm not interesting in rows with only SYSTEM_KEY = 1 like the first row in the following table because SERVER_KEY = 2 is also needed, a correct result table will only contain the two last rows in the following table because the CHAN开发者_StackOverflow社区GE_KEY 4 has both SYSTEM_KEY=1 and SERVER_KEY=2
CHANGE_KEY SYSTEM_KEY SERVER_KEY HARDWARE_KEY
----------- ----------- ----------- ------------
1 1 NULL NULL
1 NULL 1 NULL
1 NULL NULL 1
2 NULL 1 NULL
3 NULL 1 NULL
4 NULL 1 NULL
5 NULL 1 NULL
5 NULL 1 NULL
4 NULL 2 NULL
4 1 NULL NULL
Unless I'm missing something, it's as easy as this.
Select * From MyTable Where Change_Key = 4 AND (System_Key = 1 OR System_Key = 2)
SELECT * FROM
table
WHERE CHANGE_KEY = 4
AND
(
SYSTEM_KEY = 1
OR
SERVER_KEY = 2
)
EDIT: I made changes, following your edit.
SELECT *
FROM mytable
WHERE CHANGE_KEY = 4
AND (SYSTEM_KEY = 1
OR SERVER_KEY = 2)
Edit: You've edited the question to say that the last two rows in the table should be in the result-set. However, an AND
will not achieve this, I believe you're looking for an OR so have modified my response.
SELECT * FROM your_table WHERE change_key = 4 AND system_key = 1 AND server_key = 2
In MySql, i would write something like this:
SELECT * FROM table_name WHERE CHANGE_KEY=4 AND SYSTEM_KEY=1 AND SERVER_KEY=2;
SELECT * FROM table_name WHERE change_key = 4 AND system_key = 1 AND system_key = 2
精彩评论