0

例子:

id  name  agreement
32  fahid   52
43  nahid   51
31  zal     52

想通过协议选择,但我想保留给定的顶部id,如果我使用,order by那么总是对 id 进行排序。如果我搜索agreement=52然后id=32预期结果:

32 fahid 52
31 fahid 52
4

3 回答 3

1

尝试这个

DECLARE @id INT
DECLARE @agreement INT

SET @id = 32
SET @agreement = 52

SELECT [id]
, (SELECT [name] FROM Table1 WHERE [id] = @id) AS [name]
, [agreement] FROM Table1 
WHERE [agreement] = @agreement

SQL 小提琴演示

输出

id  name    agreement
32  fahid   52
31  fahid   52
于 2013-09-12T07:09:05.490 回答
1

您可以按顺序使用多个字段 order by id,agreement

于 2013-09-12T06:59:26.033 回答
0

在 mysql 中使用 LIMIT

MySQL 语法

SELECT column_name(s)FROM table_name WHERE (condition) LIMIT number;

例子

SELECT * FROM Persons WHERE agreement=52 LIMIT 5;

甲骨文语法

SELECT column_name(s)FROM table_name WHERE ROWNUM <= number; 

例子

SELECT * FROM Persons WHERE ROWNUM <=5;
于 2013-09-12T07:09:26.580 回答