我有这个声明在没有以下声明的情况下工作正常。
我想我可能正在使用不正确的陈述。我尝试做的只是选择第一个 productsapplied.applicationid 。如果另一行具有与已选择的 productsapplied.applicationid 相同的行,则不会选择它。可以有多个相同的应用程序ID,但我只需要添加1.
DISTINCT(' productsapplied' .applicationid)
'SELECT `productsApplied`.id, DISTINCT(`productsApplied`.applicationid)
FROM `productsapplied`
INNER JOIN `products`
ON `productsApplied`.productid = `products`.id
INNER JOIN `applications`
ON `productsApplied`.applicationid = `applications`.id
WHERE `applications`.clubid = ? AND `applications`.area = ? AND EXTRACT(YEAR FROM `applications`.date) = ? AND `products`.producttype = ?
如果有人有任何想法,会很感激!
如果不是,我只想做COUNT DISTINCT
让我们说这是应用的产品
id:3 clubid:6 applicationid:5 ...
id:4 clubid:6 applicationid:5 ...
id:5 clubid:6 applicationid:5 ...
id:4 clubid:6 applicationid:6 ...
假设声明的其余部分为例如年=?等等。
然后返回的行数为2. AS有3行具有相同的应用程序ID。无论有多少行具有相同的应用程序ID,都应计算一个。
答案 0 :(得分:1)
DISTINCT
适用于整行但不适用于您需要关注的问题,每productsApplied.id
会有不同的applicationid
s,因此您可以使用group_concat
抓取所有内容
SELECT
GROUP_CONCAT(pa.id),
`pa`.applicationid
FROM
`productsapplied` pa
INNER JOIN `products` p
ON `pa`.productid = `p`.id
INNER JOIN `applications` a
ON `pa`.applicationid = `a`.id
WHERE `a`.clubid = ?
AND `a`.area = ?
AND EXTRACT(YEAR FROM `a`.date) = ?
AND `p`.producttype = ?
GROUP BY `pa`.applicationid
如果您不关心productsApplied.id
s,那么您可以简单地按部分使用该组,但是没有聚合函数的注释组将导致不确定的顺序
修改强>
这将为每个applicationid提供一个最大ID
SELECT
SUBSTRING_INDEX(GROUP_CONCAT(pa.id ORDER BY pa.id DESC), 1),
`pa`.applicationid
FROM
`productsapplied` pa
INNER JOIN `products` p
ON `pa`.productid = `p`.id
INNER JOIN `applications` a
ON `pa`.applicationid = `a`.id
WHERE `a`.clubid = ?
AND `a`.area = ?
AND EXTRACT(YEAR FROM `a`.date) = ?
AND `p`.producttype = ?
GROUP BY `pa`.applicationid
或
SELECT
MAX(pa.id),
`pa`.applicationid
FROM
`productsapplied` pa
INNER JOIN `products` p
ON `pa`.productid = `p`.id
INNER JOIN `applications` a
ON `pa`.applicationid = `a`.id
WHERE `a`.clubid = ?
AND `a`.area = ?
AND EXTRACT(YEAR FROM `a`.date) = ?
AND `p`.producttype = ?
GROUP BY `pa`.applicationid
答案 1 :(得分:0)
Select min(pa.id), pa.applicationid
From productsapplied pa
Join ...
...
Group by pa.applicationid