我正在尝试优化我的查询,我的想法是使用EXISTS子句查找我正在寻找的现有帐户。这就是我想出的:
CREATE VIEW recruit_rev AS
SELECT p.clinic, CONCAT(p.last_name, ', ', p.first_name) AS doctor, p.email, p.hire_date, p.practice_date, q.period_id, q.fiscal_year, SUM(t.amount) AS production
FROM transactions t
INNER JOIN provider p
ON (t.clinic=p.clinic AND p.provider_id=t.provider_id)
CROSS JOIN period q
WHERE (t.time_ran BETWEEN q.period_start AND q.period_end)
AND impacts='P'
AND EXISTS (
SELECT p.provider_id
FROM provider p
WHERE (p.clinic=t.clinic AND p.provider_id=p.provider_id)
AND p.position_id=1
AND p.email LIKE '%-Time%')
GROUP BY p.clinic, p.email, p.hire_date, p.practice_date, q.period_id, q.fiscal_year, p.first_name, p.last_name
但是这不起作用,我正在使用的当前工作查询只是将过滤器作为普通查询的一部分而没有EXISTS。
AND p.position_id=1
AND p.email LIKE '%-Time%'
有什么见解,我转错了?提前谢谢。
答案 0 :(得分:3)
不要使用相同的别名," p"在这种情况下,在内部子查询和外部范围中。
AND EXISTS (
SELECT p.provider_id
FROM provider p2
WHERE (p2.clinic=t.clinic AND p2.provider_id=p.provider_id)
AND p2.position_id=1
AND p2.email LIKE '%-Time%')
答案 1 :(得分:0)
除非你的数据库设计是非典型的,provider
表中的SELECT p.clinic,
CONCAT(p.last_name,', ',p.first_name) AS doctor,
p.email,
p.hire_date,
p.practice_date,
q.period_id,
q.fiscal_year,
SUM(t.amount) AS production
FROM transactions t
INNER JOIN provider p ON p.provider_id = t.provider_id)
INNER JOIN period q ON t.time_ran BETWEEN q.period_start AND q.period_end
WHERE impacts = 'P'
AND p.position_id = 1
AND p.email LIKE '%-Time%'
GROUP BY p.clinic,
p.email,
p.hire_date,
p.practice_date,
q.period_id,
q.fiscal_year,
p.first_name,
p.last_name
每行(提供者)不是唯一的,否则你的查询应该看起来像这样。
EXISTS
如果provider_id
可以在provider
表格中多次使用不同的设置,您只能使用max_restarts: 3, max_seconds: 5
,并且如果任何不同的设置与您的过滤器相匹配,您希望包含提供商