我知道从开始日期和结束日期开始,您可以使用connect by
选择该范围内的所有日期。但是我很难将其概括为多个开始和结束日期。这是一个简化的例子(使用数字而不是日期):
select
name,
min + level
from (
select 'foo' as name, 0 as min, 5 as max from dual
union all
select 'bar' as name, 10 as min, 20 as max from dual
)
connect by nocycle
(prior name = name) and level <= (max - min);
我希望得到15行,但我只得到2.简化查询:
select
name,
min + level
from (
select 'foo' as name, 0 as min, 5 as max from dual
)
connect by nocycle
level <= (max - min);
我按预期得到了五行。但以下只返回一个:
select
name,
min + level
from (
select 'foo' as name, 0 as min, 5 as max from dual
)
connect by nocycle
(prior name = name) and level <= (max - min);
同样,莫名其妙地,以下内容:
select
name,
min + level
from (
select 'foo' as name, 0 as min, 5 as max from dual
)
connect by nocycle
(prior name = name or 1=1) and level <= (max - min);
这里发生了什么,我该如何解决?
答案 0 :(得分:1)
补救措施:请勿使用$username = "Doe";
$logname = "John";
$query=mysqli_query($db_conx, "SELECT s.id, s.account_name, s.author, s.data, s.postdate FROM status s INNER JOIN friends f ON f.user1='$username' OR f.user2='$username' WHERE s.account_name='$username' OR s.author='$username' OR s.account_name='$logname' OR s.author='$logname' GROUP BY s.id ORDER BY s.postdate DESC");
//I have also tried these two queries but not giving me what I want
//$query = mysqli_query($db_conx, "SELECT * FROM status WHERE account_name = '$username' OR author = '$username' ORDER BY postdate DESC");
$num_row = mysqli_num_rows($query);
echo " Numbers ".$num_row;
//$query =mysqli_query($db_conx, "SELECT s.* , f.* FROM status s, friends f WHERE s.account_name='$user' AND f.user1='$user' OR f.user2='$user'");
while($row=mysqli_fetch_array($query))
{
?>
<tr>
<td><p><?php echo $row['data']; ?></p></td>
<td><p><?php echo $row['id']; ?></p></td>
<td><p><?php echo $row['author']; ?></p></td>
<td><p><?php echo $row['account_name']; ?></p></td>
</tr>
<?php
}
。要打破循环,请使用PRIOR添加一个条件,这将强制每行中有一个唯一的附加(系统提供)列。标准为connect by nocycle
说明:而不是重新发明方向盘,https://community.oracle.com/thread/2526535
重写第一个查询:
... and prior sys_guid() is not null