这个MySQL查询是最好的主意吗?

ide*_*lop 1 php mysql optimization

我正在处理的项目有两种类型的帐户," people"和" companies".

我持有一个users包含所有帐户的单个" "表格,只有登录所需的基本信息(电子邮件,通行证等),以及另外两个表格" user_profiles"(普通人)和" company_profiles"(公司),每个表格包含更多特定列type,两个users表通过" profile_user_id"列链接到常规" "表.

但是,每当我想列出可以是人和公司的用户时,我都会使用:

" select user_id, user_type, concat_ws('', concat_ws(' ', user_profiles.profile_first_name, user_profiles.profile_last_name), company_profiles.profile_company_name) as user_fullname".

当我列出这些用户时,我知道他们是"人"还是公司user_type.

我的方法是使用concat_ws正确的(最佳)方法吗?我做了这个而不是select每次*_name都避免返回超过必要的列.

谢谢

编辑:上面的查询继续如下: from users left join user_profiles on ... left join company_profiles on ...

mso*_*son 5

select
 u.user_id, u.user_type, concat_ws(profile_first_name + profile_last_name) as full_name
from 
 users u, user_profiles up
where u.key = up.key
 and u.user_type = 'user'

union

select
 u.user_id, u.user_type, concat_ws(profile_first_name + profile_last_name) as full_name
from 
 users u, company_profiles cp
where u.key = cp.key
 and u.user_type = 'company'
Run Code Online (Sandbox Code Playgroud)