我是
MySQL的初学者我只知道完全基本的陈述,但现在我有时间进入一些更困难但更有价值的东西.
我实际上在MySQL中有3个表,这里是表示:
用户:
user_id | name | country
---------------------------
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,
ment,
notes.country,
admins.name,
admins.country
FROM notes, admins
WHERE notes.n_id = admin.admin_id
ELSEIF notes.type = 'user'
SELECT
notes.note,
ment,
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.