Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

mysql - 1 प्रदाता तालिका से 2 अलग प्रदाता नाम दिखाएं

आप इसका इस्तेमाल कर सकते हैं:

select se.svcid, p1.pname as serviceprovider, p2.pname as tmidprovider
from tmid t
inner join (select sno, max(svcid) as maxsvcid from service group by sno) s 
  on t.sno = s.sno and t.`default` = 'Y'
inner join service se on se.svcid = s.maxsvcid
left join provider p1 on se.pid = p1.pid
left join provider p2 on t.pid = p2.pid;

तालिका

create table service (svcid int, sno varchar(20), pid int);
insert into service values 
(1, '11-11-11-11', 1), (2, '11-11-11-11', 2), (3, '11-11-11-12', 1), (4, '11-11-11-12', 2), (5, '11-11-11-13', NULL);

create table tmid (id int, sno varchar(20), pid int, `default` char(1));
insert into tmid values
(1, '11-11-11-11', 1, 'N'),(2, '11-11-11-11', 2, 'Y'),(3, '11-11-11-12', 1, 'N'),
(4, '11-11-11-12', 2, 'Y'),(5, '11-11-11-13', 2, 'Y'),(6, '11-11-11-13', 3, 'N');

create table provider (pid int, pname varchar(20));
insert into provider values (1, 'Ambank'), (2, 'Citybank'), (3, 'CIMB Bank');

डेटा

mysql> select * from service;
+-------+-------------+------+
| svcid | sno         | pid  |
+-------+-------------+------+
|     1 | 11-11-11-11 |    1 |
|     2 | 11-11-11-11 |    2 |
|     3 | 11-11-11-12 |    1 |
|     4 | 11-11-11-12 |    2 |
|     5 | 11-11-11-13 | NULL |
+-------+-------------+------+

mysql> select * from tmid;
+------+-------------+------+---------+
| id   | sno         | pid  | default |
+------+-------------+------+---------+
|    1 | 11-11-11-11 |    1 | N       |
|    2 | 11-11-11-11 |    2 | Y       |
|    3 | 11-11-11-12 |    1 | N       |
|    4 | 11-11-11-12 |    2 | Y       |
|    5 | 11-11-11-13 |    2 | Y       |
|    6 | 11-11-11-13 |    3 | N       |
+------+-------------+------+---------+

mysql> select * from provider;
+------+-----------+
| pid  | pname     |
+------+-----------+
|    1 | Ambank    |
|    2 | Citybank  |
|    3 | CIMB Bank |
+------+-----------+

परिणाम

+-------+-----------------+--------------+
| svcid | serviceprovider | tmidprovider |
+-------+-----------------+--------------+
|     2 | Citybank        | Citybank     |
|     4 | Citybank        | Citybank     |
|     5 | NULL            | Citybank     |
+-------+-----------------+--------------+



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. जब यह NULL हो तो MAX फ़ंक्शन के साथ 0 कैसे सेट करें?

  2. एसक्यूएल स्टेटमेंट में घटाव

  3. MySQL तालिका (php) में एक साधारण सूची सम्मिलित करना

  4. जहां मौजूद नहीं है वहां डालें-प्राथमिक कुंजी के बिना

  5. मैक्स फंक्शन जहां क्लॉज mysql