简体   繁体   English

PHP MySQL - 查找行ID

[英]PHP MySQL - Find Row ID

I have a table called "participants" that has 3 fields: 我有一个名为“参与者”的表,有3个字段:
prt_id prt_id
prt_event_id prt_event_id
prt_participant_id prt_participant_id

What I have is a select query with a where condition on event_id. 我所拥有的是一个在event_id上具有where条件的select查询。 The query returns let's say 20 rows (20 different participants). 查询返回让我们说20行(20个不同的参与者)。 What I would like to do is to be able to figure out the row number for a given participant (prt_id). 我想做的是能够找出给定参与者的行号(prt_id)。

SELECT * 
FROM participants
WHERE prt_id = someinteger

While you can't specifically find a row ID using MySQL, you could do something like the following: 虽然您无法使用MySQL专门找到行ID,但您可以执行以下操作:

$conn = new mysqli(/*dbinfo*/);
$res = $conn->query("SELECT prt_id FROM participants");

$rowids = array(); $currid = 1;
while ($row = $res->fetch_object()) { // this is using the mysqli library
  $rowids[$row->prt_id] = $currid;
  $currid++;
}

This would give you an array of ids associated with prt_id. 这将为您提供与prt_id相关联的一组ID。

This should do what you want inside MySQL (ie assign a rownum in the order of prt_id), but the performance will be dependent on the number of rows in the table so it's not optimal. 这应该在MySQL中做你想要的(即按照prt_id的顺序分配rownum),但性能将取决于表中的行数,因此它不是最佳的。

SELECT * FROM (
  SELECT @tmp:=@tmp+1 rownum, p.*
  FROM (SELECT @tmp:=0) z, participants p
  ORDER BY prt_id
) participants
WHERE prt_id = 36;

Demo here . 在这里演示。

Edit: This "doh level" rewrite uses an simple index range instead of a table scan, so should be much faster (provided prt_id is a PRIMARY KEY) 编辑:这个“doh级别”重写使用一个简单的索引范围而不是表扫描,所以应该更快(假设prt_id是一个PRIMARY KEY)

SELECT *, COUNT(p2.prt_id) ROWNUM
FROM participants p1
JOIN participants p2
  ON p1.prt_id >= p2.prt_id
WHERE p1.prt_id=36;

Demo here . 在这里演示。

you could just add an index column in your database, set it as int, primary key and auto increment. 您可以在数据库中添加索引列,将其设置为int,主键和自动增量。 then when retrieving the row you retrieve the index number. 然后在检索行时检索索引号。

RowID is a feature of Oracle: http://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns008.htm . RowID是Oracle的一项功能: http//docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns008.htm

MySQL does not have something like that, you can basically emulate that by assign number to an array inside php as you retrieve each row, but that doesn't guarantee you the same number next time you retrieve that results. MySQL没有类似的东西,你基本上可以通过在检索每一行时为php中的数组赋值来模拟它,但这并不能保证下次检索结果时你得到相同的数字。 You probably have to settle for using one of the primary IDs 您可能不得不满足于使用其中一个主要ID

You could do something like: 你可以这样做:

<?php

$counter = 1; // Start at one for first entry

$res = mysql_query("SELECT * FROM participants WHERE prt_id = 12");

while( $array = mysql_fetch_assoc($res) )
{
    // Do something with the counter, store it into array with details
    $counter++;
}
?>

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

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