0

I'm ahaving the problem in mysql query,,I need mysql query for display the following output and also i need to take csv or excel or pdf report .

Table1:

id : nos
---------
1    12,13,14
2    14
3    14,12

Table2:

id : values
------------
12   raja
13   rames
14   ravi

I want output like this:

id  values
---------------------
1   raja
1   rames
1   ravi
2   ravi
3   ravi
3   raja
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
raja
  • 35
  • 2
  • 6
  • 2
    You should consider to normalize your tables. Storing several values in one column is bad practice. – Marcus Nov 11 '11 at 08:52
  • A similar question was asked yday by you - http://stackoverflow.com/questions/8075323/mysql-with-comma-separated-values - have you tried writing any queries? – Jan S Nov 11 '11 at 08:57
  • yes jan but the output slightly differ – raja Nov 11 '11 at 08:58
  • yes jan i am trying to execute differnt queries... – raja Nov 11 '11 at 08:59
  • 1
    @raja: Try to use the `FIND_IN_SET()` function: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_find-in-set – ypercubeᵀᴹ Nov 11 '11 at 09:39

3 Answers3

1

The following query will split out the list, but its not likely to perform well on a large table.

SELECT table1.id, table2.values
FROM table1
    JOIN table2 ON CONCAT(',', table1.nos, ',') LIKE CONCAT(',' table2.id, ',')
a'r
  • 35,921
  • 7
  • 66
  • 67
0

if you have nos1, nos2, nos3 ... ecc

SELECT a.id,a.nos
        b.id,b.values
FROM Table1 as a, Table2 as b
WHERE (a.nos1 = b.id) OR
   (a.nos2 = b.id) OR
   (a.nos3 = b.id)
0

You need to save your data in table 1 in multiple records. for example data will be

id : nos
---------
1    12
1    13
1    14
2    14
3    14
3    12

Then you can use following query to get your result

select table1.id, table2.values from table1,table2 where table1.nos=table2.id
AaA
  • 3,600
  • 8
  • 61
  • 86