我想得到表中存在的行数。
+---+----+
|id |name|
+---+----+
|100|a |
+---+----+
|201|b |
+---+----+
|302|c |
+---+----+
|403|d |
+---+----+
|504|e |
+---+----+
在上表中,我希望输出为4(即)该行的计数存在。我有'd'值并且必须编写查询以将输出设为4 其中name = d
我认为代码将类似于下面的内容,
select count(*) ......
答案 0 :(得分:0)
SELECT ROW_NUMBER() OVER(ORDER BY id) FROM yourtable WHERE name='d'
MySQL版
SET @rank=0;
SELECT @rank := @rank+1 AS rank
FROM yourtable WHERE name='d'
ORDER BY id asc
答案 1 :(得分:0)
获取行号
SELECT ROW_NUMBER() OVER(ORDER BY id) from table WHERE name='d'
要使用您的条件获取行数,请使用以下查询
select count(*) as count from table where name = 'b'
答案 2 :(得分:0)
如果我正确理解你,那么这个问题就是你想要的:
set @row_number = 0;
select @row_number := @row_number + 1 as row_number,name FROM table_name;
答案 3 :(得分:0)
@vinoth我认为@Pragmatist答案对你有用。只需将此子句添加到他的查询中:
set @row_number = 0;
select @row_number := @row_number + 1 as row_number,name FROM table_name Where name='b';
答案 4 :(得分:0)
尝试以下方法。 MS SQL是一个经过测试的查询,我刚刚转换成了mysql。希望您可以根据您的要求进行修改。
SET @rank=0;
SELECT * FROM Table1 T1
INNER JOIN(
SELECT @rank := @rank+1 AS rank, ID
FROM Table1
ORDER BY id asc) temp ON temp.ID = T1.ID
WHERE T1.name = 'd'
MS SQL查询将
SELECT * FROM Table1 T1
INNER JOIN(SELECT id, ROW_NUMBER() OVER (Order by id) AS RowNumber from Table1) temp ON temp.ID = T1.ID
WHERE T1.name = 'd'