我正在尝试从users
的表中进行子选择。
但我得到一个错误说
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS n1 ) LIMIT 0, 25' at line 7
我不明白我在这里做错了什么
SELECT * FROM ( SELECT
n1.userid,
n1.country,
n1.gender
FROM users
WHERE n1.country = 'US'
AND n1.gender = '1' AS n1 )
答案 0 :(得分:6)
我认为这就是你想要的:
SELECT *
FROM (SELECT n1.userid, n1.country, n1.gender
FROM users n1
WHERE n1.country = 'US' AND n1.gender = '1'
) n1;
as
不属于WHERE
条款。如果要使用它来限定列名,则需要n1
作为子查询中的表别名。并且,您应该为外部查询提供表别名。
注意:显然不需要子查询,但我猜你真的想要它来解决你的实际问题。
答案 1 :(得分:1)
你确实把别名放在了错误的地方。尝试
SELECT * FROM (SELECT
n1.userid,
n1.country,
n1.gender
FROM users AS n1 -- <AS n1> this should be here
WHERE n1.country = 'US'
AND n1.gender = '1')
或
SELECT * FROM (SELECT
n1.userid,
n1.country,
n1.gender
FROM users
WHERE n1.country = 'US'
AND n1.gender = '1') AS n1 -- or here
但是,如果查询不仅仅是一个简化的表单,那么整个查询也可以在没有子查询的情况下编写:
SELECT userid, country, gender
FROM users
WHERE country = 'US'
AND gender = '1'
答案 2 :(得分:1)
它应该如下所示,因为AS n1
指的是内联视图别名。同样,您尝试使用相同的别名来引用子查询中的列,这是另一个错误。
SELECT * FROM ( SELECT
userid,
country,
gender
FROM users
WHERE country = 'US'
AND gender = '1') AS n1