简体   繁体   English

如何在MYSQL到select中使用CASE语句从基于条件的结果中获取一行?

[英]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?我是 sql 的新手。我有一个 SELECT 查询,它返回 2 行,其中有一个 rid(两行均为 1)和一个路径列,其二进制值为 0 或 1 我想使用 CASE 语句,这样它将 SELECT 一行从基于条件s1.pid < s2.pid THEN (SELECT S2.rid WHERE r.path = 0) ELSE (SELECT S2.rid WHERE r.path=1 )的第一个查询的结果中,下面的代码返回两行 rid 1 和路径 0 和 1 我如何编写案例陈述? I dont know the correct syntax.我不知道正确的语法。

Schema: database schema架构:数据库架构

  • stopno rid busno pid buskey path stopno rid busno pid 总线键路径
    4 1 1111 4 key1 0 4 1 1111 4 键 1 0
    4 1 2222 4 key2 1 4 1 2222 4 键 2 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?这是我从运行第一个查询得到的结果,现在我想要 select 基于条件的行之一 where s1.pid< s2.pid 然后 select 行 where path = 0 否则我该怎么做?

$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'.我已经试过了,但它给了我错误 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? s2的scope是什么? 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?编辑 2:我试过下面的代码,它返回 p1.pid 和 p2.pid 但在 IF 语句中给出语法错误,任何人都可以解决它吗?

$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'我使用了具有相同stop表的自连接并将其别名为s1并将连接条件用作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.您还可以进一步简化它,而不是使用多个 IF 或 CASE 语句。

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)您可以使用r.path in (0,1)来定义它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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