[英]Is this MySQL query the best idea?
我正在處理的項目有兩種類型的帳戶,“ people
”和“ companies
”。
我擁有一個帶有所有帳戶的“ users
”表,僅包含登錄所需的基本信息(電子郵件,通行證等),還有另外兩個表“ user_profiles
”(常規人員)和“ company_profiles
”(公司)擁有更具體的信息每種類型的列,兩個表都通過“ profile_user_id
”列鏈接到常規“ users
”表。
但是,每當我要列出既可以是個人又可以是公司的用戶時,我會使用:
“ 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
的方法是否正確(最佳)? 我這樣做是為了避免返回*_name
列,而不是對每個*_name
select
-ing。
謝謝
編輯:上面的查詢繼續像: from users left join user_profiles on ... left join company_profiles on ...
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'
您已經查詢成功了嗎? 您是否已經通過使用這種方法遇到性能問題?
除非使用上面的查詢花費的時間比您預期的要長,或者導致調用此信息的軟件出現問題,否則這可能是過早的優化。
不過要注意一點,您第一次使用CONCAT_WS時沒有分隔符,因此公司名稱將與該人的姓名合並。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.