Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL从table2中选择数据,其中table1.field相等(并将其添加到结果中)_Mysql_Sql - Fatal编程技术网

MySQL从table2中选择数据,其中table1.field相等(并将其添加到结果中)

MySQL从table2中选择数据,其中table1.field相等(并将其添加到结果中),mysql,sql,Mysql,Sql,我有一个Profiles表,一个Workers表和一个Studios表 我的配置文件表如下所示: mysql> select * from Profiles; | profile_id | owner_luser_id | profile_type | profile_name | profile_entity_name | description | profile_image_url | search_rating | my Studios和Workers表基本上都有关于实体

我有一个Profiles表,一个Workers表和一个Studios表

我的配置文件表如下所示:

mysql> select * from Profiles;
    | profile_id | owner_luser_id | profile_type | profile_name | profile_entity_name | description | profile_image_url | search_rating |
my Studios和Workers表基本上都有关于实体的扩展数据,具体取决于将哪个概要文件类型(ENUM)设置为[Worker,Studio]

我需要知道如何通过一个SQL查询从概要文件中检索(所有)概要文件数据,但根据实体是Studio还是Worker,只连接到结果的末尾,即Studio或Workers表中的特定字段

我尝试了UNION,但没有成功(UNION要求所有表具有相同数量的字段?,这似乎不是我想要的)

我也尝试过加入,但没有成功;如果我加入Profiles,它只显示Worker。profile_type='Worker'

我只是希望能够根据Profiles.profile_类型,从Worker或Studio表向结果中添加额外的列


我的模式的SQLFiddle位于-我想(例如)从配置文件中选择*,但根据配置文件类型的不同,在结果的末尾加入工作者或工作室的许可证号(分别为Workers.license\u号、Studios.premises\u许可证号),并将该列称为“profilelicense\u number”。

如果要使用外部联接,则会联接存在的数据,如果不存在,则使用空行联接

SELECT * FROM Profiles p
    LEFT JOIN Studios s on p.profile_id = s.profile_id
    LEFT JOIN Workers w on p.profile_id = w.profile_id
或者,如果需要所有配置文件数据,但不需要所有工作室和工人数据,则可以使用
p.*
后跟工作室和工人的特定列,例如:

SELECT p.*, s.phone, w.phone FROM Profiles p
    LEFT JOIN Studios s on p.profile_id = s.profile_id
    LEFT JOIN Workers w on p.profile_id = w.profile_id

我想这就是你想要的:

SELECT p.*,
       case
         when w.license_number is not null then
          w.license_number
         when s.premises_license_number is not null then
          s.premises_license_number
         else
          null
       end as profilelicense_number
  FROM Profiles p
  LEFT JOIN Studios s
    on p.profile_id = s.profile_id
  LEFT JOIN Workers w
    on p.profile_id = w.profile_id

您可以使用case语句将额外的数据合并到一列中,如下所示

SELECT p.profile_id,
case p.profile_type
  when 'Studio' then s.premises_license_number
  when 'Worker' then w.license_number
  else ''
  end as profilelicense_number
from Profiles p
left outer join studios s on p.profile_id = s.profile_id
left outer join workers w on p.profile_id = w.profile_id

这并不能解释同时存在s.premises\u许可证号码和w.license\u号码的情况,它只会显示找到的第一个号码。

考虑提供适当的DDL(和/或SQLFIDLE)以及所需的结果集What is Workers and Studios table?更新后的帖子和提供的sql FIDLE!谢谢:)@Anthony,你读过我的答案了吗?在大多数情况下,使用select*是不可取的-除非你真的想要那个电话的每个栏目,否则你是在浪费内存。当然,他没有指定他需要哪些栏目-但是如果我在回答(因为你几秒钟就赢了我,所以我没有)我要提到这一点,所以没有人回答说这是一个好的标准,这正是我所需要的:)是的,我绝对不会使用*,这只是一个传达想法的例子!太好了@AnthonyMusgrove如果希望一个列显示一个字段,如果使用了S连接,如果使用了W连接,则需要一个不同的字段,那么必须使用CASE语句(请参见我的回答)。