简体   繁体   English

MySQL:在select语句中使用if else进行子查询

[英]MySQL: Subquery in the select statement using if else

I want to display a list of members and their status ( uid, uname, uAddress , status). 我想显示成员及其状态的列表(uid,uname,uAddress和status)。 I want to check where the uid in the T1 table exists in the T2 table ie (T1.uid = T2.uid and tl_u_id='3') if exists then status will be yes otherwise no. 我想检查T1表中的uid在T2表中是否存在,即(T1.uid = T2.uid和tl_u_id ='3')如果存在,则状态为是,否则为否。 (tl_u_id column value should be hardcoded in the query) (tl_u_id列值应在查询中进行硬编码)

 Table T1 (Primary key: uid)                  Table T2 (primary_key: Aid)

        --------------------      ---------------------------
        uid   uname  uAddress      uid  Aid  tl_u_id  ename
        --------------------      ---------------------------
         1      aa     ch           2    1    3        TG
         2      bb     LA           4    2    3        IS
         3      cc     NY           2    3    4        DS
         4      dd     DC
        --------------------      --------------------------

Result for tl_u_id=3
-------------------------
uid uname uAddress status
-------------------------
1    aa      ch      No
2    bb      LA      Yes
3    cc      NY      No
4    dd      DC      yes

Please provide me the best way to do this. 请提供给我最好的方法。

There are several approaches to getting the result set. 有几种获取结果集的方法。

Using a correlated subquery can be quick, if you are returing a small set of rows. 如果要重现少量行,则使用关联子查询可能会很快。

SELECT u.uid
     , u.uname
     , u.uAddress
     , IFNULL(
         (SELECT 'Yes'
            FROM T2 s 
           WHERE s.uid = t.uid
             AND s.tl_u_id = '3'
           LIMIT 1
         )
       ),'No') AS status
  FROM T1 u

But that's not necessarily the best way to do get the resultset, for anything but small sets (due to the way that MySQL processes that subquery, for each row in the outer query), that can be expensive for large sets. 但这不一定是获取结果集的最佳方法,除了小集合(由于MySQL处理外部查询中每一行的子查询的方式)以外,对于大集合而言,这可能是昂贵的。

Another way, if you are returning all, or a large percentage of, the rows from T1, and there aren't a lot of values of uid in T2 that don't match a uid value in T1, this can be much more efficient: 另一种方式,如果你正在返回所有或大比例的,从T1行,并且不会有很多值的uid在T2不匹配T1一个UID值,这样可以更有效:

SELECT u.uid
     , u.uname
     , u.uAddress
     , IF(s.uid IS NOT NULL,'Yes','No') AS status
  FROM T1 u
  LEFT
  JOIN (SELECT r.uid
          FROM T2 r
         WHERE r.tl_u_id = '3'
         GROUP BY r.uid
       ) s
    ON s.uid = u.uid

If you have a guarantee that T2(uid,tl_u_id) is unique, or at least there will not be any duplicates for a given uid with tl_u_id='3' , then you could get better performance by eliminating the inline view. 如果您保证T2(uid,tl_u_id)是唯一的,或者至少对于tl_u_id='3'的给定uid不存在任何重复项,则可以通过消除内联视图来获得更好的性能。

For optimum performance, you'll likely want an index ... ON T2 (tl_u_id, uid) . 为了获得最佳性能,您可能需要索引... ON T2 (tl_u_id, uid)

Here is a simple way of doing it. 这是一种简单的方法。 Select first table and join the second one if record is coming from table 2 the dislay Yes as status else no. 选择第一个表,如果记录来自表2,则加入第二个表,将显示为状态,否则为否。

SELECT
    T1.uid,
    T1.uname,
    T1.uAddress
    IF(T2.uid IS NULL ,'No','Yes') as `Status`
FROM    T1
LEFT JOIN T2 ON T1.uid = T2.uid 
AND T2.tl_u_id='3'
SELECT  a.*,
        CASE 
            WHEN b.uid  IS NULL 
            THEN 'NO' 
            ELSE 'YES' 
        END Status
FROM    tableT1 a
        LEFT JOIN
        (
            SELECT  DISTINCT uid, tl_u_id  -- <<== filter duplicates
            FROM    tableT2
        ) b ON  a.uid = b.uid AND
                b.tl_u_id = 3

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

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