簡體   English   中英

MySQL INSERT…SELECT。 在HAVING子句中使用count(column)但不使用列

[英]MySQL INSERT … SELECT. Using count(column) but not column works in HAVING clause

運行時:

SELECT '2000-01-01 00:00:00', 223
FROM timeslots
LEFT JOIN timeslotUsers ON timeslots.start = timeslotUsers.start
WHERE timeslots.start = '2000-01-01 00:00:00'
HAVING count(user_id) < timeslots.capacity

我收到ERROR 1054 (42S22): Unknown column 'timeslots.capacity' in 'having clause'

我了解HAVING作用於查詢后的值,因此,像這樣可以為SELECT添加容量:

SELECT '2000-01-01 00:00:00', 223, timeslots.capacity

問題是,我真正想做的是:

INSERT INTO timslotUsers (start, user_id)
SELECT '2000-01-01 00:00:00', 223
FROM timeslots
LEFT JOIN timeslotUsers ON timeslots.start = timeslotUsers.start
WHERE timeslots.start = '2000-01-01 00:00:00'
HAVING count(user_id) < timeslots.capacity

據我所知, select參數必須與insert參數匹配,因此我無法添加capacity 我不確定為什么count(user_id)select參數having沒有user_id的情況下起作用。 count()之外的user_id也抱怨user_id為“未知”。 所以我猜count()以某種方式獲取值。 有沒有類似的功能可以簡單地獲取capacity的價值,因此我可以在having功能時使用它? 運動having條款列入where抱怨使用count()

總體來說,我想要做到的是有條件的INSERT INTO timeslotUsers依賴於相應的變量timeslots行被比較的總數timeslotUsers具有匹配timeslots.start 如果可能的話,我希望在沒有子查詢的情況下執行此操作。

我想知道的事情:為什么count(user_id)HAVING起作用,而單獨的timeslots.capacitytimeslots.user_id卻不起作用? 它本身是否在運行子查詢? 有沒有允許我使用capacity的函數,例如count()允許我使用user_id

編輯:

樣本數據:

SELECT * FROM timeslotUsers;
+---------------------+---------+
| start               | user_id |
+---------------------+---------+
| 2000-01-01 00:00:00 |     227 |
| 2000-01-01 00:00:00 |     228 |
| 2000-01-01 00:00:00 |     229 |
| 2000-01-01 00:00:00 |    2234 |
+---------------------+---------+
SELECT * FROM timeslots;
+---------------------+---------------------+-----------+----------+
| start               | end                 | location  | capacity |
+---------------------+---------------------+-----------+----------+
| 2000-01-01 00:00:00 | 2000-01-01 02:00:00 | someplace |        5 |
+---------------------+---------------------+-----------+----------+

當我在上面運行插入操作時,我希望將另一行放置在timeslotUsers ,但timeslotUsers是在timeslotUsers具有匹配start行少於capacity

EDIT2:

哇。 這可行。 (已更改為FROM timeslots並增加了GROUP BY capacity

INSERT INTO timeslotUsers (start, user_id)
SELECT timeslots.start, 223
FROM timeslots
LEFT JOIN timeslotUsers ON timeslots.start = timeslotUsers.start
WHERE timeslots.start = '2000-01-01 00:00:00'
GROUP BY capacity
HAVING COUNT(user_id) < capacity;

問為什么甚至合理?

在WHERE子句上將HAVING更改為子選擇即可。 這未經測試,但應該接近:

INSERT INTO timeslotUsers (start, user_id) SELECT '2000-01-01 00:00:00', 223
FROM timeslots LEFT JOIN timeslotUsers ON timeslots.start = timeslotUsers.start 
WHERE timeslots.start = '2000-01-01 00:00:00' AND (SELECT COUNT(*) from 
timeslotUsers t where t.start = '2000-01-01 00:00:00') < timeslots.capacity

這是一個不使用子查詢的Messenger版本

INSERT INTO timeslotUsers (start, user_id) SELECT '2000-01-01 00:00:00', 223
FROM timeslots LEFT JOIN timeslotUsers on timeslots.start = timeslotUsers.start 
WHERE timeslots.start = '2000-01-01 00:00:00' GROUP BY timeslots.start, 
timeslots.capacity HAVING SUM(CASE WHEN timeslotUsers.start IS NULL THEN 0 ELSE 
1 END) < timeslots.capacity

這有點令人討厭,但也應該起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM