0

我是休眠的新手。这是一张User桌子

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(16) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| sex   | char(1)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

这是我的样本数据

+----+-------+------+------+
| id | name  | age  | sex  |
+----+-------+------+------+
|  1 | Amit  |   20 | M    |
|  2 | Sumit |   21 | M    |
|  3 | Mohan |   22 | M    |
|  4 | Ram   |   26 | M    |
|  5 | John  |   22 | M    |
|  6 | Sita  |   19 | F    |
+----+-------+------+------+

这些是我的疑问:

select id from User where name="Amit" and age=20;
select id from User where name="Ram" and age=26;
select id from User where name="Mohan" and age=22;
select id from User where name="Sita" and age=19;

我可以通过运行 4 个查询在休眠状态下做到这一点。有什么方法可以让我在一个查询中做到这一点?请告诉同样可以在mysql中完成。请帮忙。任何线索、链接或参考都将是可观的。谢谢你。

4

2 回答 2

2

你可以这样做

select  id , name ,age , sex from User 
where (name,age) IN (('Amit','20'),('Ram', '26'),('Mohan', '22'),('Sita', '19'));

在这里演示

于 2013-10-01T19:31:15.547 回答
1

在 sql...

select name, id from User where ( name="Amit" and age=20)
                             or ( name="Ram" and age=26 )
                             or ( name="Mohan" and age=22 )
                             or ( name="Sita" and age=19 )
于 2013-10-01T19:25:43.220 回答