5

我有带有odd_id 的表,我想为不同的ticket_id 选择组合。这是我的查询:

SELECT
ticket_id,
GROUP_CONCAT(odd_id) as oddsconcat
FROM ticket_odds
GROUP BY ticket_id

它给了我以下信息:

'28', '14472510,14472813,14472546,14472855,14472746,14472610,14472647'
'29', '14471149,14471138,14471125,14475603'
'30', '14471823,14471781,14471655,14471865,14471597,14471968,14471739,14471697,14471923'
'31', '14473874,14473814,14473862,14473838,14473790,14473802,14473826,14473850'
'32', '14471588,14472766,14471651,14471777,14471419'
'33', '14472647,14472605,14471650,14471734'
'34', '14471588,14472704,14471817'
'35', '14475279,14474499'
'282', '14472756,14472652,14472813'
'283', '14471588,14472766,14471419,14471777,14471651'
'284', '14474521'
'285', '14474529'
'286', '14474547'
'287', '14471134,14471199,14473636,14471242,14471398,14471237'

但是,如果我使用具有功能,它不会给我结果。显然:它给了我以下内容:

SELECT
ticket_id,
GROUP_CONCAT(odd_id) as oddsconcat
FROM ticket_odds
GROUP BY ticket_id
HAVING oddsconcat = '14475279,14474499'

返回ticket_id 35 并且每个人都很高兴并且代码工作正常,但是如果oddsconcat 大于这个,它不会返回任何值。例如:

SELECT
ticket_id,
GROUP_CONCAT(odd_id) as oddsconcat
FROM ticket_odds
GROUP BY ticket_id
HAVING oddsconcat = '14473874,14473814,14473862,14473838,14473790,14473802,14473826,14473850'
4

2 回答 2

17

我会将其重写为:

SELECT 
ticket_id, 
GROUP_CONCAT(DISTINCT odd_id ORDER BY odd_id ASC) as oddsconcat 
FROM ticket_odds 
GROUP BY ticket_id 
HAVING oddsconcat = .....

现在输出oddsconcat是确定性的,因为重复项被消除并且项目按升序排列。
这应该使匹配更容易。

于 2011-10-27T13:51:00.990 回答
1

假设(ticket_id, odd_id)组合为UNIQUE,这将为您提供包含这两个赔率(可能还有更多其他赔率)的所有门票:

SELECT
  ticket_id,
  GROUP_CONCAT(odd_id) as oddsconcat
FROM ticket_odds
WHERE odd_id IN (14475279,14474499)
GROUP BY ticket_id

这将为您提供所有包含这两个赔率(且没有其他赔率)的门票:

SELECT
  ticket_id,
  GROUP_CONCAT(odd_id) as oddsconcat
FROM ticket_odds
WHERE odd_id IN (14475279,14474499)     --- list
GROUP BY ticket_id
HAVING COUNT(*) = 2                     --- size of list
于 2011-10-27T14:01:32.983 回答