简体   繁体   English

mysql从表中获取行号,其中列值等于特定值

[英]mysql get row number from table where a column value is equal to a specific value

I have a table where results of modeltest are given in a column in descending order. 我有一张表,其中test的结果在列中按降序给出。 Now I want to get the row number of a specific row. 现在,我想获取特定行的行号。 Here is the example table: 这是示例表:

在此处输入图片说明

So when i will search a with userid=44, it should return 3. Again when i will search with userid=11, it should return 4. How can I do this ? 因此,当我将使用userid = 44搜索a时,它应该返回3。再次,当我将使用userid = 11搜索时,它应该返回4。我该怎么办? If anyone could kindly help me out. 如果有人能帮助我。

A simple where clause will achieve this. 一个简单的where子句将实现此目的。

SELECT id FROM table WHERE userid = 44

Swap table and userid value to whatever is needed. 将表和用户标识值交换为所需的值。

Hope this helps 希望这可以帮助

for example: 例如:

$stmt = PDO::prepare('SELECT id FROM table WHERE userid = ?');
$userId = 44;

$stmt->execute([$userId]);
$result = $stmt->fetch(PDO::FETCH_OBJ);

echo $result->id;

http://php.net/manual/en/book.pdo.php http://php.net/manual/en/book.pdo.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM