简体   繁体   中英

How to select second record after first from mysql base

1. p1 | cat   | mause
2. p2 | mause | cat

Hello, I'd like to select 2 records form database with condition. My query:

SELECT * FROM mytable 
WHERE (p1 LIKE cat OR p2 LIKE cat) AND (p1 LIKE mause OR p2 LIKE mause)

ok this query will select 2 rows, it will select every row which contains "cat" and "mause" and this is a problem because I want to select only first row because first I select "cat" and next in p3 is "mause".

In the second row "mause" is at p1 column so this row shouldn't be selected because "cat" is after "mause".

__ _ __ _ __ _ _ SOME UPDATES TO MY QUESTION _ __ _ __ _ __ _ ___

Below is My example of table from database. All of records are definied as VARCHAR. I must write link because I have low reputation in stackoverflow and I can't pase image here.

Link to image with database

I want to select the route from Las Vegas to Chicago. But when I do this query:

    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)

it will select all of the rows which contains Las Vegas and Chicago, but I don't want to select the route where Chicago is first in the database. I only want to select this routes where Las Vegas is before Chicago in the table. So it will be only first row the "row 1".

In row 2 Las Vegas is after Chicago (Las Vegas station number 3, Chicago station 2) so I don't want to, the same at row 3.

I think it's now clear for everyone.

Not quite sure I understand what you mean, but I guess! Is this OK?:

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%')
          )

In order to generate such a WHERE clause, you can to something like:

$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 .= ')';
}

尝试在查询末尾添加LIMIT 0、1。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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