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

Use Select data in nested Select statement in MySQL

我在 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 table.
获取数据 现在我的要求是我需要找到特定 tour_sub_code 的总出席人数。可以从 attendance_master table 中找到 that 特别 tour_sub_code 的出席人数。 所以我包含了一个嵌套的 select 查询,它是

嵌套select

(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' 

现在这个查询给我错误,因为

Unknown column 'tc.tour_sub_code' in 'where clause'

预期结果

Expense              Tour_sub_code          login_id            Count

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

我能知道我哪里出错了吗?
还有其他方法可以通过使用 JOINS 获得结果吗?

试试这个:

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

试试这个 ---

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' 

你能试试这个吗:

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' 

在最内层的子查询中,您引用了最外层查询中的列。 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'