2

NOTE : I tried many SF solution, but none work for me. This is bit challenging for, any help will be appreciated.

Below is my SQL-Fiddle link : http://sqlfiddle.com/#!9/6daa20/9

I have tables below:

CREATE TABLE `tbl_pay_chat` (
nId int(11) NOT NULL AUTO_INCREMENT,
npayid int(11) NOT NULL,
nSender int(11) NOT NULL,
nTos varchar(255) binary DEFAULT NULL,
nCcs varchar(255) binary DEFAULT NULL,
sMailBody varchar(500) binary DEFAULT NULL,
PRIMARY KEY (nId)
)
ENGINE = INNODB,
CHARACTER SET utf8,
COLLATE utf8_bin;

INSERT INTO tbl_pay_chat
(nId,npayid,nSender,nTos,nCcs,sMailBody)
 VALUES
(0,1,66,'3,10','98,133,10053','Hi this test maail'),
(0,1,66,'3,10','98,133,10053','test mail received');

 _____________________________________________________________

CREATE TABLE `tbl_emp` (
empid int(11) NOT NULL,
fullname varchar(45) NOT NULL,
PRIMARY KEY (empid)
)
ENGINE = INNODB,
CHARACTER SET utf8,
COLLATE utf8_bin;



INSERT INTO `tbl_emp` (empid,fullname)
VALUES
(3, 'Rio'),
(10, 'Christ'),
(66, 'Jack'),
(98, 'Jude'),
(133, 'Mike'),
(10053, 'James');

What I want :

  1. JOIN above two tables to get fullname in (nTos & nCcs) columns.

  2. Also, I want total COUNT() of rows.

What I tried is below query but getting multiples time FULLNAME in 'nTos and nCcs column' also please suggest to find proper number of row count.

 SELECT a.nId, a.npayid, e1.fullname AS nSender, sMailBody, GROUP_CONCAT(b.fullname ORDER BY b.empid) 
 AS nTos, GROUP_CONCAT(e.fullname ORDER BY e.empid) AS nCcs
 FROM    tbl_pay_chat a
    INNER JOIN tbl_emp b
        ON FIND_IN_SET(b.empid, a.nTos) > 0
    INNER JOIN tbl_emp e 
        ON FIND_IN_SET(e.empid, a.nCcs) > 0
    JOIN tbl_emp e1
        ON e1.empid = a.nSender
 GROUP   BY a.nId ORDER BY a.nId DESC;

I hope I made my point clear. Please help.

sober
  • 23
  • 4
  • See https://meta.stackoverflow.com/questions/333952/why-should-i-provide-a-minimal-reproducible-example-for-a-very-simple-sql-query, although I suspect that at its heart, this is a problem of poor schema design. (https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad) – Strawberry Oct 13 '20 at 07:26
  • @Strawberry, I understood but my question is very simple, and I am not allowed to share my real table data. I am very close to my task what I tried, if you can see. Also if possible please suggest possible changes, I will make the changes. – sober Oct 13 '20 at 07:31
  • Could you add your expected result as well ? – Sujitmohanty30 Oct 13 '20 at 07:34
  • Does your query gives the output which you need? – Akina Oct 13 '20 at 07:34
  • @Sujitmohanty30 what is want is fullname instead of 'ids' in 'nTos & nCcs' column, also I pasted SQL FIDDLE link , you can check. – sober Oct 13 '20 at 07:38
  • I think that `GROUP_CONCAT(DISTINCT ...)` is more safe... https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=346cda45606402c7b0242f2490e34b59 – Akina Oct 13 '20 at 07:38
  • See normalisation. Ccs and tos need to be in a separate table – Strawberry Oct 13 '20 at 07:40
  • @Akina, I am very close to my result, just need some help,I atatched SQL fiddle link, where I you can see my working. But I am getting repeated names as well as If I make count function use, it gives wrong COUNT. – sober Oct 13 '20 at 07:41
  • @Akina, yes I am almost close, distinct helps for me. Now I just want no. of rows return. from SQL fiddle SQL query i shud get 2 but getting 6 rows in return. – sober Oct 13 '20 at 07:44
  • Provide desired output, add it into the question text. PS. The fiddle in my comment gives 2 output rows - but you need 6 rows? – Akina Oct 13 '20 at 07:45
  • In both fiddle provided by you and @Akina do give 2 rows. What is the issue then ? – Sujitmohanty30 Oct 13 '20 at 07:46
  • @Akina, In simple terms I want no. of rows return, I don't want 6 rows , I want 2 rows only. But how to use COUNT() function to get no. of rows. – sober Oct 13 '20 at 07:48
  • I do not see desired output in your question. Now it is absolutely unclear WHERE do you want to see this `2 rows` and in what form. – Akina Oct 13 '20 at 07:49
  • @Sujitmohanty30, yes it gives 2 rows, but how to use COUNT() to get no. of rows. If I use COUNT() function, I get 6 as values return instead if 2. My desired result for row count is 2 only. – sober Oct 13 '20 at 07:50
  • *If I use COUNT() function, I get 6 as values return instead if 2.* ?? https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=d795c6d118eddc4372ca0f7dd9b8f45f – Akina Oct 13 '20 at 07:52
  • @Akina, I want total number of rows. PLease check latest SQL-Fiddle link and check last output result, where i am getting 6 as row count, instead I want 2 as rows count. – sober Oct 13 '20 at 07:53
  • @Akina, please check this http://sqlfiddle.com/#!9/6daa20/9 and check last output, in total_rows, I am getting as 6 , but it shud return 2. – sober Oct 13 '20 at 07:55
  • You cannot. This count is a data from another grouping level (the query performs grouping by nId whereas COUNT() needs above grouping level). If your server version is 8+ then you may use window version of COUNT(), if not then you need to create liiogical synthetic expression which may fail anytime when some special relations occures. – Akina Oct 13 '20 at 07:57
  • @Sujitmohanty30, can you check this http://sqlfiddle.com/#!9/6daa20/9 and see last output result in total_rows in that I am getting 6 as row-count where I shud get as 2. – sober Oct 13 '20 at 07:58

1 Answers1

0

You have a horrible data model. You should not be storing lists of ids in strings. Why? Here are some reasons:

  • Numbers should be stored as numbers not strings.
  • Relationships between tables should be declared using foreign key relationships.
  • SQL has pretty poor string manipulation capabilities.
  • The use of functions and type conversion in ON often prevents the use of indexes.

No doubt there are other good reasons. Your data model should be using properly declared junction tables for the n-m relationships.

That said, sometimes we are stuck with other people's really, really, really, really bad design decisions. There are some ways around this. I think the query that you want can be expressed as:

 SELECT pc.nId, pc.npayid, s_e.fullname AS nSender, pc.sMailBody,
        GROUP_CONCAT(DISTINCT to_e.fullname ORDER BY to_e.empid) 
 AS nTos,
        GROUP_CONCAT(DISTINCT cc_e.fullname ORDER BY cc_e.empid) AS nCcs
 FROM tbl_pay_chat pc INNER JOIN
      tbl_emp to_e
      ON FIND_IN_SET(to_e.empid, pc.nTos) > 0 INNER JOIN
      tbl_emp cc_e
      ON FIND_IN_SET(cc_e.empid, pc.nCcs) > 0 JOIN
      tbl_emp s_e
      ON s_e.empid = pc.nSender
 GROUP BY pc.nId
 ORDER BY pc.nId DESC;

Here is a db<>fiddle.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Hi first of all thank you for response and suggestion. I really apprecited your valuable feedback and I do use it in my upcoming query. And Second @Akina Solved my query in comments. And I am new to this MY_SQL. Can you please tell me how to insert comma separated number in INT datatype? – sober Oct 15 '20 at 11:59
  • @sober . . . I think you should ask a new question. – Gordon Linoff Oct 15 '20 at 12:51