我有SQL select查询,我使用内部连接,我得到一些值,但有重复。我想要独特的价值观,而且我正在使用DISTINCT,但它无效。
select DISTINCT
cy_affiliates_clickthroughs_tbl.*,
cy_affiliates_tbl.email,
cy_affiliates_tbl.firstname
from
cy_affiliates_clickthroughs_tbl
inner join cy_affiliates_tbl on
cy_affiliates_clickthroughs_tbl.refid = cy_affiliates_tbl.referrer
where
cy_affiliates_clickthroughs_tbl.refid = '" . $_SESSION['user_id'] . "'
AND date BETWEEN '$start_date' AND date BETWEEN '$start_date'
AND '$end_date'
ORDER BY date DESC", OBJECT;
我想获得不同的电子邮件和名字值。
答案 0 :(得分:0)
您应该从选择中删除cy_affiliates_clickthroughs_tbl.*
。
如果您只想选择不同的电子邮件和名字,那就是您应该拥有的。
你的where子句中也可能存在问题,因为date BETWEEN '$start_date' AND date BETWEEN '$start_date' AND '$end_date'
是错误的。
你可能意味着date BETWEEN '$start_date' AND '$end_date'
。
答案 1 :(得分:-1)
您可以使用GROUP BY获取名称和电子邮件的不同值:
select
cy_affiliates_tbl.email,
cy_affiliates_tbl.firstname,
cy_affiliates_clickthroughs_tbl.*
from
cy_affiliates_clickthroughs_tbl
inner join cy_affiliates_tbl on
cy_affiliates_clickthroughs_tbl.refid = cy_affiliates_tbl.referrer
where
cy_affiliates_clickthroughs_tbl.refid = '" . $_SESSION['user_id'] . "'
AND date BETWEEN '$start_date'
AND '$end_date'
GROUP BY cy_affiliates_tbl.email, cy_affiliates_tbl.firstname
ORDER BY date DESC", OBJECT;
请注意,您将错过cy_affiliates_clickthroughs_tbl的部分结果,以确保名称和电子邮件的DISTINCT值。