1. p1 | cat | mause
2. p2 | mause | cat
您好, 我想从条件数据库中选择2条记录。我的问题:
SELECT * FROM mytable
WHERE (p1 LIKE cat OR p2 LIKE cat) AND (p1 LIKE mause OR p2 LIKE mause)
确定此查询将选择2行,它将选择包含“cat”和“mause”的每一行,这是一个问题,因为我只想选择第一行,因为首先我选择“cat”,然后在p3中选择“mause”。
在第二行“mause”位于p1列,因此不应选择此行,因为“cat”位于“mause”之后。
的 的 __ _ __ _ __ _ _ 我的问题的一些更新 _ __ _ __ _ __ _ ___
以下是我的数据库表格示例。所有记录都被定义为VARCHAR。我必须写链接,因为我在stackoverflow中的声誉很低,我不能在这里使用图像。
我想选择从拉斯维加斯到芝加哥的路线。但是当我这样做时:
SELECT * FROM mytable
WHERE (station1 LIKE Las Vegas OR station2 LIKE Las Vegas OR station3 LIKE Las Vegas) AND (station1 LIKE Chicago OR station2 LIKE Chicago OR station3 LIKE Chicago)
它将选择包含拉斯维加斯和芝加哥的所有行,但我不想选择芝加哥在数据库中第一个的路线。我只想选择拉斯维加斯在芝加哥之前的这条路线。所以它只是第一行“第1行”。
在第2排拉斯维加斯是在芝加哥(拉斯维加斯3号站,芝加哥站2号)之后,所以我不想,在第3排也一样。
我认为现在每个人都清楚了。
答案 0 :(得分:1)
我不太清楚我明白你的意思,但我想!这样可以吗?:
SELECT *
FROM myTable
WHERE (station1 LIKE '%Las Vegas%' AND (
station2 LIKE '%Chicago%' OR
station3 LIKE '%Chicago%' OR
station4 LIKE '%Chicago%' OR
station5 LIKE '%Chicago%')
) OR
(station2 LIKE '%Las Vegas%' AND (
station3 LIKE '%Chicago%' OR
station4 LIKE '%Chicago%' OR
station5 LIKE '%Chicago%')
) OR
(station3 LIKE '%Las Vegas%' AND (
station4 LIKE '%Chicago%' OR
station5 LIKE '%Chicago%')
) OR
(station4 LIKE '%Las Vegas%' AND (
station5 LIKE '%Chicago%')
)
为了生成这样的WHERE
子句,您可以使用以下内容:
$arr_sta = array('station1', 'station2', 'station3', 'station4');
$nbstations = count($arr_sta);
$where = '';
for($i=0; $i<$nbstations-1; $i++) {
$where .= $where ? ' OR (' : ' WHERE (';
$where .= $arr_sta[$i] . " LIKE '%Las Vegas%'";
$where2 = '';
for($j=$i+1; $j<$nbstations; $j++) {
$where2 .= $where2 ? ' OR ' : ' AND (';
$where2 .= $arr_sta[$j] . " LIKE '%Chicago%'";
}
if ($where2) $where .= $where2 . ')';
$where .= ')';
}
答案 1 :(得分:0)
尝试在查询结尾添加LIMIT 0,1。