[英]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.capacity
或timeslots.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.