简体   繁体   中英

MySQL: Subquery in the select statement using if else

I want to display a list of members and their 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. (tl_u_id column value should be hardcoded in the query)

 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.

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:

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.

For optimum performance, you'll likely want an index ... 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.

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

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