我被这个难住了。我尝试了几个角度,有些角度很接近,但没有一个是完全正确的。
我需要从表格的 3 个字段中进行选择。第一个是一个 log_id,每行都有一个唯一的数字,第二个是一个 entry_id,它来自另一个表并且有多个相同的条目号,最后一个是代表一种类型的单个字母。 'D' 表示相应的条目已被删除,顺便说一句。
例子:
"SELECT log_id, entry_id, type from entry_log order by entry_id";
...具有以下示例结果(实际查询的片段):
6 1 C
31 1 D
8 2 C
25 2 D
11 3 C
14 3 D
13 3 D
18 4 D
17 4 D
12 4 C
22 5 D
15 5 C
398 6 U
249 6 U
44 6 U
587 6 R
272 6 U
498 6 R
578 6 U
590 6 U
529 6 R
564 6 D
43 7 U
42 7 D
16 7 C
我需要选择行[2] 值等于“D”的每个行[1] 值的最高行[0] 值。换句话说,当 row[1] = 6 时,正确的选择是上面数组中 row[0] = 564 的位置。我需要将一个 entry_id(row[1]) 及其对应的 log_id(row[0]) 放入另一个数组中。
我尝试了以下方法:
"SELECT MAX(log_id), entry_id, type from entry_log where type = 'D'";
这没有说明第 1 行中某些条目的条目高于与 D 匹配的条目的事实(因为它们不再被删除)。
我试过倒退:
"SELECT log_id, entry_id, type from entry_log where log_id in ('U','R','C','x')";
还是不合逻辑。
$log = "SELECT log_id, entry_id, type from entry_log order by entry_id;
$lgcomp = mysql_query($log);
while ($row = mysql_fetch_array($logcomp, MYSQL_NUM)) {
$cli = $row[0];
$cei = $row[1];
$ct = $row[2];
}
foreach ($cli as $key=>$clid)
if $cei =
...难倒...
foreach($cli as $key => $clid){
$lgentry = $cei[$key];
switch($clid) {
case $lgentry = :
break;
...想不通...
我只是不具备 PHP mysql 语法的技术理解,无法正确显示。
任何和所有想法或只是为我指明正确的方向将不胜感激。不,你不需要写我的脚本(我确实喜欢弄清楚这对我来说是什么)但我已经花了很多时间在这个上,我知道我只是缺少一些语法以及了解 PHP/mysql 中的排序。就 Internet 搜索而言,我只能提出无休止的重复,在这种情况下无济于事。
谢谢!过去几周,这位 72 岁的老人在这里学到了很多东西。
最佳答案
试试这个:
select el1.* from entry_log el1
left join (
select entry_id, log_id from entry_log
where type = 'D'
) el2
on el1.entry_id = el2.entry_id and el1.log_id < el2.log_id
where el2.log_id is null and el1.type = 'D'
结果:
+--------+----------+------+ | LOG_ID | ENTRY_ID | TYPE | +--------+----------+------+ | 31 | 1 | D | | 25 | 2 | D | | 14 | 3 | D | | 18 | 4 | D | | 22 | 5 | D | | 564 | 6 | D | | 42 | 7 | D | +--------+----------+------+
关于php - 从 3 个字段中选择,第 0 行唯一 ID,第 1 行多个 ID 号,第 2 行一个字母 int(1) 到新数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9867301/