简体   繁体   中英

how to use CASE statement in MYSQL to select a row from results based on condition?

I am new to sql. I have a SELECT query that returns 2 rows which have a rid (1 in both rows) and a path column which has binary value 0 or 1 I want to use a CASE statement such that it will SELECT one row from results of first query based on condition s1.pid < s2.pid THEN (SELECT S2.rid WHERE r.path = 0) ELSE (SELECT S2.rid WHERE r.path=1 ) Below code returns two rows with rid 1 and path 0 and 1 how do i write a case statement? I dont know the correct syntax.

Schema: database schema

  • stopno rid busno pid buskey path
    4 1 1111 4 key1 0
    4 1 2222 4 key2 1

this is the result im getting from running 1st query now i want to select one of the rows based on condition where s1.pid< s2.pid then select row where path = 0 else otherwise how do i do that?

$sql="SELECT s2.stopno, s2.rid, busno, s2.pid, buskey, `path`
FROM `stop` s2
INNER JOIN `route` r ON s2.rid = r.rid
INNER JOIN bus b ON r.bid = b.bid
INNER JOIN place p ON s2.pid = p.pid
WHERE p.place = 'place2'
AND s2.rid IN (SELECT  s1.rid
                 FROM `stop` s1
                 INNER JOIN `route` r ON s1.rid = r.rid
                 INNER JOIN bus b ON r.bid = b.bid
                 INNER JOIN place p ON s1.pid = p.pid
                 AND p.place = 'place1')
";

I've tried this but it is giving me error Unknown column 's2.pid' in 'field list'. It seems that I cant access inner subquery alias in outer query or am I doing it wrong? what is the scope of s2? Is there any work around for this?

$sql=" SELECT (CASE 
WHEN s2.pid > s1.pid
THEN (SELECT s3.rid 
     FROM `stop` s3
    INNER JOIN `route` r ON s3.rid = r.rid
    INNER JOIN bus b ON r.bid = b.bid
    INNER JOIN place p ON s3.pid = p.pid
    WHERE s3.rid IN 
          (SELECT s2.rid
          FROM `stop` s2
          INNER JOIN `route` r ON s2.rid = r.rid
          INNER JOIN bus b ON r.bid = b.bid
          INNER JOIN place p ON s2.pid = p.pid
          WHERE p.place = 'place2'
          AND s2.rid IN 
                (SELECT  s1.rid
                FROM `stop` s1
                INNER JOIN `route` r ON s1.rid = r.rid
                INNER JOIN bus b ON r.bid = b.bid
                INNER JOIN place p ON s1.pid = p.pid
                AND p.place = 'place1'))
                AND r.path = '0')
ELSE (SELECT s3.rid 
      FROM `stop` s3
      INNER JOIN `route` r ON s3.rid = r.rid
      INNER JOIN bus b ON r.bid = b.bid
      INNER JOIN place p ON s3.pid = p.pid
      WHERE  s3.rid IN
              (SELECT  s2.rid
              FROM `stop` s2
              INNER JOIN `route` r ON s2.rid = r.rid
              INNER JOIN bus b ON r.bid = b.bid
              INNER JOIN place p ON s2.pid = p.pid
              WHERE p.place = 'place2'
              AND s2.rid IN 
                    (SELECT  s1.rid
                    FROM `stop` s1
                    INNER JOIN `route` r ON s1.rid = r.rid
                    INNER JOIN bus b ON r.bid = b.bid
                    INNER JOIN place p ON s1.pid = p.pid
                    AND p.place = 'place1'))
                    AND r.path = '1')
                    END)AS pathfinder
FROM `stop` s
INNER JOIN `route` r ON s.rid = r.rid
INNER JOIN bus b ON r.bid = b.bid
INNER JOIN place p ON s.pid = p.pid
";

Edit 2: I've tried code below and it is returning p1.pid and p2.pid but giving syntax error in IF statement can anyone solve it?

$sql="SELECT s2.stopno, s2.rid, b.busno, p1.pid as id1, p2.pid as id2, b.buskey, r.path, p1.place as place1, p2.place as place2,
   IF(p1.pid < p2.pid, IF(r.path=0, s2.*, null), IF(r.path=1, s2.*,null))
       
FROM `stop` s2

JOIN `route` r
ON s2.rid = r.rid
JOIN bus b 
ON r.bid = b.bid
JOIN place p1
ON s2.rid = p1.pid
JOIN place p2
ON s2.pid = p2.pid
WHERE 
p1.place = 'place1'
AND p2.place = 'place 2'

";

You description is really confusing. Anyways, I have attempted to rewrite the query. Instead of using

AND s2.rid IN (
                    SELECT  s1.rid

I have used a self join with the same stop table and alias it as s1 and used the join condition as s2.rid = s1.rid and s1.place = 'place1'

$sql="SELECT s2.stopno, s2.rid, b.busno, s2.pid, b.buskey, r.path,
        IF(s1.pid < s2.pid, IF(r.path=0, s2.rid, null), IF(r.path=1, s2.rid,null))
    FROM `stop` s2, bus b, `route` r, place p, `stop` s1
    WHERE s2.rid = r.rid
    AND r.bid = b.bid
    AND s2.pid = p.pid
    AND s2.rid = s1.rid and s1.place = 'place1'
    AND p.place = 'place2'"

You can also simplify this further instead of using multiple IF or CASE statements.

s1.pid < s2.pid THEN (SELECT S2.rid WHERE r.path = 0) ELSE (SELECT S2.rid WHERE r.path=1 )

You can define it using r.path in (0,1)

IF(s1.pid < s2.pid), IF(r.path in (0,1),s2.rid, NULL), NULL)

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