我需要提供以下内容:
Comp No Year Purchased
=========================
11111 2008
22222 2007
33333 2008
44444 2006
但是我需要根据串联名称排除一些结果。而且我无法在结果中列出名称。这是我的代码:
SELECT
Comp_Num,
YEAR (Comp_PurchaseDate) AS 'Year Purchased',
CONCAT(Emp_First, ' ', Emp_Last) as 'Name'
FROM Computer
JOIN Hire using (Comp_Num)
JOIN employee using (Emp_Num)
ORDER BY Comp_Num;
它产生:
Comp No Year Purchased Name
================================
11111 2008 AAA
22222 2007 BBB
33333 2008 CCC
44444 2006 DDD
串联名称用于过滤结果,例如:
WHERE ('Name' <> 'AAA' AND
Name' <> 'DDD')
如何创建串联名称以在不显示列的情况下过滤出结果?没有“作为'name'“时,串联将无法工作。
如何使用串联名称进行过滤?我还能在哪里使用吗?还是还有另一个条款?
答案 0 :(得分:2)
您可以直接在WHERE
子句中定义表达式:
SELECT
Comp_Num,
YEAR (Comp_PurchaseDate) AS `Year Purchased`
FROM Computer
JOIN Hire using (Comp_Num)
JOIN employee using (Emp_Num)
WHERE (CONCAT(Emp_First, ' ', Emp_Last) <> 'AAA' AND
CONCAT(Emp_First, ' ', Emp_Last) <> 'DDD')
ORDER BY Comp_Num;
也请阅读:When to use single quotes, double quotes, and back ticks in MySQL
答案 1 :(得分:0)
Madhur 的两个 CONCAT()
调用的一个更简单的替代方法是使用 NOT IN ()
并且只连接一次。
SELECT Comp_Num,
YEAR (Comp_PurchaseDate) AS `Year Purchased`
FROM Computer
JOIN Hire USING Comp_Num
JOIN employee USING Emp_Num
WHERE CONCAT(Emp_First, ' ', Emp_Last) NOT IN ('AAA','DDD')
ORDER BY Comp_Num