我有這樣兩個表:需要幫助創建SQL查詢
CREATE TABLE `workers` (
`id` int(7) NOT NULL AUTO_INCREMENT,
`number` int(7) NOT NULL,
`percent` int(3) NOT NULL,
`order` int(7) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE `data` (
`id` bigint(15) NOT NULL AUTO_INCREMENT,
`workerId` int(7) NOT NULL,
PRIMARY KEY (`id`)
);
我想(通過order
ASC順序)返回第一個工人,他的表中的行數data
倍percent
(從表workers
)/100小於number
(從表workers
我曾嘗試此查詢:
SELECT workers.id, COUNT(data.id) AS `countOfData`
FROM `workers` as workers, `data` as data
WHERE data.workerId = workers.id
AND workers.percent * `countOfData` < workers.number
LIMIT 1
但我得到的錯誤:
#1054 - Unknown column 'countOfData' in 'where clause'
呃,如果不是這樣的MySQL,請用適當的RDBMS標記它。 –
您不應該將'order'作爲列名稱,因爲它是大多數SQL中的關鍵字。 – JHS
它是mysql .... Juniad - 忽略名稱 - 我們稱它爲'sortOrder' - – Nir