mysql的if函數,mysql if join_如何在MySQL中使用JOIN編寫正確的If … Else語句?

 2023-11-19 阅读 27 评论 0

摘要:我是mysql的if函數、MySQL的初學者我只知道完全基本的陳述,但現在我有時間進入一些更困難但更有價值的東西.我實際上在MySQL中有3個表,這里是表示:MySQL join?用戶:user_id | name | countrymysql行轉列,---------------------------1 | Joseph | US2 | Kenne

我是

mysql的if函數、MySQL的初學者我只知道完全基本的陳述,但現在我有時間進入一些更困難但更有價值的東西.

我實際上在MySQL中有3個表,這里是表示:

MySQL join?用戶:

user_id | name | country

mysql行轉列,---------------------------

1 | Joseph | US

2 | Kennedy | US

3 | Dale | UK

管理員:

admin_id | name | country

----------------------------

1 | David | UK

2 | Ryan | US

3 | Paul | UK

筆記:

id | n_id | note | comment | country | type | manager

----------------------------------------------------------------

1 | 3 | This is the 1st note | First | US | admin | 2

2 | 2 | This is the 2nd note | Second | US | user | 1

3 | 2 | This is the 3rd note | Third | UK | user | 2

現在我想執行類似這樣的SQL(我將在這里輸入不是真正的命令,因為我并不熟悉所有的SQL表達式):

IF notes.type = admin

THEN

SELECT

notes.note,

notes.comment,

notes.country,

admins.name,

admins.country

FROM notes, admins

WHERE notes.n_id = admin.admin_id

ELSEIF notes.type = 'user'

SELECT

notes.note,

notes.comment,

notes.country,

users.name,

users.country

FROM notes, users

WHERE notes.n_id = users.user_id

我希望你明白我想在這里實現什么.我可以使用更多的SQL語句輕松地做到這一點,但我想嘗試一些不使用那么多資源的查詢.

編輯1:

我想獲取所有Notes并獲取哪個用戶組已提交它,而不是將用戶的名稱應用于它.我的意思是,如果管理員提交了注釋,那么SQL應該從Admin表中選擇ID(根據類型值)但是如果用戶提交了注釋,它應該從Users表中獲取名稱.

結果看起來應該類似于:

result:

------

id | note | comment | country | name

--------------------------------------------------------

1 | This is the 1st note | First | US | Paul

2 | This is the 2nd note | Second | US | Kennedy

3 | This is the 3rd note | Third | UK | Kennedy

編輯2:

我實際上忘了提到,所有這些都應該列在經理身上.因此,應將“經理ID”添加到Notes中,并列出管理員所在的所有注釋:2.

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/182435.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息