SELECT s1.id,
s3.food_name,
Count(*) AS TotalRefill
FROM (SELECT ( s1.food_value - s2.food_value ) AS difference
FROM `serving_info` s1,
`serving_info` s2
WHERE s1.id - s2.id = '1'
AND s1.food_name = 'Shrimp'
AND s2.food_name = 'Shrimp') AS diff,
`serving_info` s3
WHERE s3.id = diff.id
AND s3.food_value >= '990'
AND diff.difference >= '150'
结果:#1054 - Unknown column 's1.id' in 'field list'
--
-- Table structure for table `employees`
--
CREATE TABLE IF NOT EXISTS `employees` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`email` varchar(64) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`username` varchar(16) NOT NULL,
`password` varchar(32) NOT NULL,
`confirmcode` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id_user`),
KEY (name)
);
--
-- Table structure for table `Foods`
--
CREATE TABLE IF NOT EXISTS `Foods` (
`Food_name` varchar(40) NOT NULL,
`CostPerRefill` double NOT NULL,
PRIMARY KEY (`Food_name`)
);
--
-- Table structure for table `Serving_Info`
--
CREATE TABLE IF NOT EXISTS `Serving_Info` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`Food_Value` varchar(40) NOT NULL,
`Food_name` varchar(40) NOT NULL,
`Served_On` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`Oncall` varchar(128),
Foreign key(`Oncall`) REFERENCES `employees`(`name`),
Foreign key(`Food_name`) REFERENCES `Foods`(`Food_name`),
PRIMARY KEY (`id`),
UNIQUE (`Oncall`,`Food_name`, `Served_On`)
);
是什么导致s1不被声明?由于某种原因,s1.id没有检测到s1实例。我一直试图通过改变不同的括号来解决这个问题,但我真的无法弄清楚如何调试这个..我试过改变关闭括号的位置,但这会弄乱查询。
答案 0 :(得分:1)
这里所有其他人说的是正确的,但只是为了给你一个更优先的解决方案,看下面的查询它不应该是s1.id但它应该是diff.id
SELECT diff.id,
s3.food_name,
Count(*) AS TotalRefill
FROM (SELECT ( s1.food_value - s2.food_value ) AS difference, s1.id
FROM `serving_info` s1,
`serving_info` s2
WHERE s1.id - s2.id = '1'
AND s1.food_name = 'Shrimp'
AND s2.food_name = 'Shrimp') AS diff,
`serving_info` s3
WHERE s3.id = diff.id
AND s3.food_value >= '990'
AND diff.difference >= '150'
答案 1 :(得分:0)
S1在括号内定义,因此在它们之外不可见,在那里引用它。
答案 2 :(得分:0)
因为s1仅在内部选择中定义,所以不是范围。选择diff.id或s3.id(它们将与您加入的值相同)。
答案 3 :(得分:0)
表s1不在您尝试选择它的查询的from部分,它只在子查询中。