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

अलग-अलग रिकॉर्ड में डी-सामान्यीकृत कॉलम चुनें?

केवल तभी जब आपके पास 10000 से कम ईमेल हों.... क्या यह स्वीकार्य है?

select 
       if(t1.c > 1, concat(e.employeename, ' (', e.employeeid, ')'), e.employeename) as Employee,
       replace(substring(substring_index(e.EmailAddresses, ',', n.row), length(substring_index(e.EmailAddresses, ',', n.row - 1)) + 1), ',', '') EmailAddress 
from 
       (select employeename, count(*) as c from Employees group by employeename) as t1, 
       (select EmployeeID, length(EmailAddresses) - length(replace(EmailAddresses,',','')) + 1 as emails from Employees) as t2,
       (SELECT @row := @row + 1 as row FROM 
       (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) x,
       (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) x2, 
       (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) x3, 
       (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) x4, 
       (SELECT @row:=0) as ff) as n,
       Employees e
where 
      e.employeename = t1.employeename and
      e.employeeid = t2.employeeid and
      n.row <= t2.emails
order by e.employeeid;

संपादित करें:

कम बेकार संख्या उत्पन्न होने के साथ:

select 
       if(t1.c > 1, concat(e.EmployeeName, ' (', e.EmployeeID, ')'), e.EmployeeName) as Employee,
       replace(substring(substring_index(e.EmailAddresses, ',', n.row), length(substring_index(e.EmailAddresses, ',', n.row - 1)) + 1), ',', '') as EmailAddress 
from 
       (select EmployeeName, count(*) as c from Employees group by EmployeeName) as t1, 
       (select EmployeeID, length(EmailAddresses) - length(replace(EmailAddresses,',','')) + 1 as emails from Employees) as t2,
       (select `1` as row from (select 1 union all select 2 union all select 3 union all select 4) x) as n,
       Employees e
where 
      e.EmployeeName = t1.EmployeeName and
      e.EmployeeID = t2.EmployeeID and
      n.row <= t2.emails
order by e.EmployeeID;

और हमने क्या सीखा? खराब डेटाबेस डिज़ाइन के परिणामस्वरूप भयानक प्रश्न होते हैं। और आप SQL के साथ सामान कर सकते हैं, जो शायद केवल इसलिए समर्थित हैं क्योंकि लोग खराब डेटाबेस डिज़ाइन करते हैं... :)




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. जावा में mysql डेटाबेस में एक समय सम्मिलित करना

  2. JSON फ़ाइल के लिए PHP mySql डेटा

  3. मैनुअल का उपयोग करते हुए कई अद्यतन शर्तों के साथ कोडनिर्देशक मॉडल जहां कथन

  4. MySQL में दिए गए दायरे में क्वेरी पॉइंट्स

  5. नाउ () उदाहरण – MySQL