关于数据库的SQL查询

时间:2017-09-01 17:12:13

标签: sql database

我在SQL上遇到了一些麻烦。 我有如下的数据库结构 enter image description here 我应该写一个返回的查询:所有电影明星的姓名和地址,他们是女性(G =' F')和地址="柏林" +所有工作室的名称和地址,地址="柏林"太。按地址订购。

我的建议是:

SELECT * FROM
   (SELECT name, address 
   FROM MovieStar 
   WHERE gender = 'F'
   UNION 
   SELECT name, address FROM Studio) T 
WHERE T.address LIKE '%Berlin%' ORDER BY T.address;

2 个答案:

答案 0 :(得分:1)

这应该适合你。

SELECT name, address 
   FROM MovieStar 
   WHERE gender = 'F' AND address LIKE '%Berlin%'
   UNION 
   SELECT name, address FROM Studio WHERE address LIKE '%Berlin%' 
ORDER BY address;

答案 1 :(得分:0)

你可以这样做

SELECT name, address from MovieStar WHERE gender='F' AND address LIKE '%Berlin%'
UNION 
SELECT name, address from Studio WHERE address LIKE '%Berlin'

如果你想在你的例子中使用子查询,我认为你必须为子查询添加“as”关键字

SELECT * FROM
   (SELECT name, address 
   FROM MovieStar 
   WHERE gender = 'F'
   UNION 
   SELECT name, address FROM Studio) AS T 
WHERE T.address LIKE '%Berlin%' ORDER BY T.address;

但是我不确定,这是一个很好的性能,因为你将创建一个内存表,然后进行预测