SELECT
tc.expense AS Expense
,tc.`tour_sub_code`
,tc.login_id
,tc.currency
,tc.amount
,tc.date
,tc.from_date
,tc.to_date
,tc1.NoOfDays
,tc1.NoOfDays*tc.amount totalAmount
FROM tc_wallet tc ,
(SELECT
DATEDIFF(
STR_TO_DATE(to_date, '%Y-%m-%d'),
STR_TO_DATE(From_date, '%Y-%m-%d')
) NoOfDays
FROM
tc_wallet
WHERE expense = 'DAILY ALLOWANCE'
AND login_id = 'sunny.thangaraj') AS tc1
WHERE tc.login_id = 'sunny.thangaraj'
AND tc.expense = 'DAILY ALLOWANCE'
在上述查询中,我两次从同一表中选择数据
1)各个列数据
2)同一张表的各列之间的DateDiff并使用它来计算总金额
但是它给了我两次输出,
输出
DAILY ALLOWANCE TCEB161017 sunny.thangaraj USD 50 12-02-2018 2018-12-08 2018-12-10 2 100
DAILY ALLOWANCE TCEB161017 sunny.thangaraj USD 50 12-02-2018 2018-12-08 2018-12-10 2 100
DAILY ALLOWANCE TCEB010915 sunny.thangaraj USD 2 17-01-2014 2018-12-08 2018-12-10 2 4
DAILY ALLOWANCE TCEB010915 sunny.thangaraj USD 2 17-01-2014 2018-12-08 2018-12-10 2 4
就像TOURSUBCODE TCEB010915,TCEB161017一样,这是相同的两次,是不希望的。
谁能告诉我我在做什么错吗?
是因为我正在做从A选择,(从A选择)
答案 0 :(得分:1)
您可以使用DISTINCT
SELECT DISTINCT
tc.expense AS Expense
,tc.`tour_sub_code`
,tc.login_id
,tc.currency
,tc.amount
,tc.date
,tc.from_date
,tc.to_date
,tc1.NoOfDays
,tc1.NoOfDays*tc.amount totalAmount
FROM tc_wallet tc ,
(SELECT
DATEDIFF(
STR_TO_DATE(to_date, '%Y-%m-%d'),
STR_TO_DATE(From_date, '%Y-%m-%d')
) NoOfDays
FROM
tc_wallet
WHERE expense = 'DAILY ALLOWANCE'
AND login_id = 'sunny.thangaraj') AS tc1
WHERE tc.login_id = 'sunny.thangaraj'
AND tc.expense = 'DAILY ALLOWANCE'
答案 1 :(得分:1)
删除重复项的最简单方法是使用SELECT DISTINCT而不是SELECT。
但是,您最好尝试改进查询逻辑。在这里,我看不到使用子查询的充分理由,只需一次选择il就可以了,如下所示;
SELECT
tc.expense AS Expense
,tc.`tour_sub_code`
,tc.login_id
,tc.currency
,tc.amount
,tc.date
,tc.from_date
,tc.to_date
,DATEDIFF(
STR_TO_DATE(tc,to_date, '%Y-%m-%d'),
STR_TO_DATE(tc.From_date, '%Y-%m-%d') NoOfDays
,DATEDIFF(
STR_TO_DATE(tc.to_date, '%Y-%m-%d'),
STR_TO_DATE(tc.From_date, '%Y-%m-%d')* tc.amount totalAmount
FROM tc_wallet tc
WHERE tc.login_id = 'sunny.thangaraj'
AND tc.expense = 'DAILY ALLOWANCE'