我需要使用 if 子句从我的表中获取数据,这是我正在尝试做的一个示例;
select if (condition='example',
(/*query1(return more than one row)*/),
(/*query2(return more than one row)*/) as data from table1 where iduser='1'
我的完整查询:
SELECT
if(bagian='DOKTER',
(SELECT b.ruper
from bsl_ruang b
join mr_ranap_dokter d
on (b.noregis=d.noregis)
where d.dokter = '999999'),
(SELECT u.nama
FROM m_unit u
join muser_tdinas t
on(t.unit=u.unit)
where id_user = '999999')) as ruper
FROM `muser` where id_user = '999999'
我的桌子;
bsl_ruang mr_ranap_dokter
++========++======++ ++========++========++
||noregis ||ruper || ||dokter ||noregis ||
++========++======++ ++========++========++
||10000000||ruper1|| ||9999999 ||10000000||
||10000001||ruper2|| ||9999999 ||10000001||
++========++======++ ++========++========++
m_unit muser_tdinas m_user
++========++======++ ++========++========++ ++========++=======++
||unit ||nama || ||id_user ||unit || ||id_user ||bagian ||
++========++======++ ++========++========++ ++========++=======++
||00001 ||ruper1|| ||8888888 ||00001 || ||9999999 ||DOKTER ||
||00002 ||ruper2|| ||8888888 ||00002 || ||8888888 ||PERAWAT||
++========++======++ ++========++========++ ++========++=======++
概括
所以if bagian=dokter
,它将加入表bsl_ruang
并mr_ranap_dokter
得到ruper
和if bagian=perawat
,它将加入表m_unit
并muser_tdinas
得到nama
但是 if 子句只能返回一个数据,有没有其他方法可以做到这一点?请注意,我无法更改我的数据库