简体   繁体   中英

Php Mysql 3 table Join

and i have some difficulty with php. I have 3 Tables. Table 1 Reference_categories My Table 2 ReferenceTable 3 ref_sel_categories I would like to do inner join between tables, but I couldn't help, can you help?

In the query I wrote below, only ids are coming. I want category_name to come.

Database Tables

D b

$query = $db->from('reference')
    ->select('reference.*, GROUP_CONCAT(ref_sel_categories.reference_category_id SEPARATOR \' | \') as categories')
    ->join('ref_sel_categories', 'FIND_IN_SET(%s.reference_id, %s.reference_id)')
    ->orderby('reference_order', 'ASC')
    ->groupBy('reference_id')
    ->all();
?php foreach ($query as $row): ?>
                    <tr>
                        <td width="90"><?= $row['reference_id'] ?></td>
                        <td><?= $row['reference_title'] ?>
                        </td>
                        <td width="200"><?= $row['categories'] ?></td>
                    </tr>
<?php endforeach; ?>

print_r('$row['categories']')
ref_sel_categories is coming The reference_category_id with

I solved my own question.. maybe it helps others.

$query = $db->from('reference')
    ->select('reference.*, GROUP_CONCAT(reference_categories.category_name SEPARATOR \' | \') as categories')
    ->join('ref_sel_categories', 'reference.reference_id = ref_sel_categories.reference_id ')
    ->join('reference_categories', 'reference_categories.category_id = ref_sel_categories.reference_category_id')
    ->orderby('reference_order', 'ASC')
    ->groupBy('reference_id')
    ->all();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM