0

This is my table look like

Table A-----------------
ID . Color . IDDOCT
 1 .   Red . 3
 2 .  Blue . 2,4
 3 .  Cyan . 4
------------------------

Table B-----------------
ID . NAME
 1 . HAARIS
 2 . STEAVE
 3 . DONY
 4 . CLAYS
------------------------

What i'm asking is can i get the name of Table B on multiple ID in Table A and i need to show up on table php.

  • 2
    you have to read about [SQL Joins](http://www.sql-join.com/sql-join-types/) – TarangP Dec 19 '18 at 04:27
  • how to join field with multiple id? – Muhamad Haris Setiawan Dec 19 '18 at 04:30
  • 1
    review [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad?noredirect=1&lq=1) – Sean Dec 19 '18 at 04:40
  • Possible duplicate of [mysql join on csv field?](https://stackoverflow.com/questions/21009872/mysql-join-on-csv-field) – Sean Dec 19 '18 at 04:41

2 Answers2

1

This one is tested and works just fine:

$sql = 'SELECT table_a.color AS a_color, GROUP_CONCAT(table_b.name) AS b_names FROM table_a JOIN table_b ON FIND_IN_SET(table_b.id, table_a.iddoct) GROUP BY table_a.color';
$query = $this->db->query($sql);
$result = $query->result_array();
Sherif Salah
  • 2,085
  • 2
  • 9
  • 21
0

You can try this query :

SQL :

SELECT A.ID,A.Color, group_contact(B.NAME)
FROM A 
LEFT JOIN B ON FIND_IN_SET(A.IDDOCT, B.ID)

I Hope It will help you..

NikuNj Rathod
  • 1,663
  • 1
  • 17
  • 26