在MySQL的嵌套Select语句中使用Select数据

时间:2019-04-03 06:54:19

标签: mysql sql sqlyog

我在MySQL中有一个查询。

完整查询

SELECT 
  tc.expense AS expense,
  tc.tour_sub_code,
  tc.login_id

FROM
  tc_wallet tc 
WHERE tc.login_id = 'vinod.kumbala' 
  AND tc.expense = 'Daily Allowance' 
  AND tc.delete_flag = 'F' 
  AND tc.status != 'reject' 

结果

Expense              Tour_sub_code          login_id 

DAILY ALLOWANCE       MOS-EUROPE100119      vinod.kumbala
DAILY ALLOWANCE       Test                  vinod.kumbala

最初,我是从tc_wallet表中获取数据。
现在,我的要求是我需要找到特定tour_sub_code的出勤总数。可以从attendance_master表中找到特定于tour_sub_code 的出席人数。 因此,我包含了一个嵌套的选择查询

嵌套选择

(SELECT 
    COUNT(*) 
  FROM
    (SELECT 
      * 
    FROM
      `attendance_master` 
    WHERE `delete_flag` = 'F' 
      AND login_id = 'vinod.kumbala' 
      AND `tour_sub_code` = tc.`tour_sub_code` 
    GROUP BY `device_date`) t1) AS newNoOfdays 

完整查询

SELECT 
      tc.expense AS expense,
      tc.tour_sub_code,
      tc.login_id,
      (SELECT 
        COUNT(*) 
      FROM
        (SELECT 
          * 
        FROM
          `attendance_master` 
        WHERE `delete_flag` = 'F' 
          AND login_id = 'vinod.kumbala' 
          AND `tour_sub_code` = tc.`tour_sub_code` 
        GROUP BY `device_date`) t1) AS newNoOfdays 

    FROM
      tc_wallet tc 
    WHERE tc.login_id = 'vinod.kumbala' 
      AND tc.expense = 'Daily Allowance' 
      AND tc.delete_flag = 'F' 
      AND tc.status != 'reject' 

现在此查询给我错误为

“ where子句”中的未知列“ tc.tour_sub_code”

预期结果

Expense              Tour_sub_code          login_id            Count

    DAILY ALLOWANCE       MOS-EUROPE100119      vinod.kumbala     20
    DAILY ALLOWANCE       Test                  vinod.kumbala     44

我能知道我要怎么做吗?
另外还有其他方法可以通过使用JOINS获得结果吗?

4 个答案:

答案 0 :(得分:1)

尝试一下---

SELECT 
      tc.expense AS expense,
      tc.tour_sub_code,
      tc.login_id,
      (SELECT 
        COUNT(am.*) 
      FROM `attendance_master` as am
        WHERE am.delete_flag = 'F' 
          AND am.login_id = 'vinod.kumbala' 
          AND am.tour_sub_code = tc.tour_sub_code
        GROUP BY am.device_date) AS count 

    FROM
      tc_wallet tc 
    WHERE tc.login_id = 'vinod.kumbala' 
      AND tc.expense = 'Daily Allowance' 
      AND tc.delete_flag = 'F' 
      AND tc.status != 'reject' 

答案 1 :(得分:1)

在最里面的子查询中,您引用的是最外面的查询中的一列。这对于FROM子句(派生表)中的子查询是不允许的。但是-您不需要该子查询。您需要的是COUNT(DISTINCT device_date)

重写

(SELECT 
    COUNT(*) 
  FROM
    (SELECT 
      * 
    FROM
      `attendance_master` 
    WHERE `delete_flag` = 'F' 
      AND login_id = 'vinod.kumbala' 
      AND `tour_sub_code` = tc.`tour_sub_code` 
    GROUP BY `device_date`) t1) AS newNoOfdays 

(SELECT 
    COUNT(DISTINCT device_date)
  FROM `attendance_master` 
  WHERE `delete_flag` = 'F' 
    AND login_id = 'vinod.kumbala' 
    AND `tour_sub_code` = tc.`tour_sub_code` 
) AS newNoOfdays 

您还可以将完整查询重写为LEFT JOIN查询:

SELECT 
  tc.expense AS expense,
  tc.tour_sub_code,
  tc.login_id,
  COUNT(DISTINCT device_date) AS newNoOfdays 
FROM tc_wallet tc
LEFT JOIN attendance_master am
  ON  am.tour_sub_code = tc.tour_sub_code
  AND am.delete_flag = 'F' 
  AND am.login_id = 'vinod.kumbala' 
WHERE tc.login_id = 'vinod.kumbala' 
  AND tc.expense = 'Daily Allowance' 
  AND tc.delete_flag = 'F' 
  AND tc.status != 'reject' 

答案 2 :(得分:0)

尝试一下:

SELECT 
      tc.expense AS expense,
      tc.tour_sub_code,
      tc.login_id,
      count(*) as count

    FROM
      tc_wallet tc
    INNER JOIN
      attendance_master am
    ON
      tc.tour_sub_code = am.tour_sub_code AND
      tc.login_id = am.login_id

    WHERE tc.login_id = 'vinod.kumbala' 
      AND tc.expense = 'Daily Allowance' 
      AND tc.delete_flag = 'F' 
      AND tc.status != 'reject'

    GROUP BY 
      tc.expense AS expense,
      tc.tour_sub_code,
      tc.login_id

答案 3 :(得分:0)

您能尝试一下吗?

select  tc.expense AS expense,
        tc.tour_sub_code,
        tc.login_id,
        newNoOfdays.countVal
from    tc_wallet tc
inner join
        (   select  tour_sub_code, count('A') 'countval'
            from    attendance_master ascs (nolock)
            group by tour_sub_code) as newNoOfdays
on  newNoOfdays.tour_sub_code = tc.tour_sub_code
where   tc.login_id = 'vinod.kumbala' 
AND     tc.expense = 'Daily Allowance' 
AND     tc.delete_flag = 'F' 
AND     tc.status != 'reject'