我有两张桌子:
表1: option_value
| option_value_id | option_id | sort_order |
|=================|===========|============|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
表2: option_value_description
| option_value_id | option_id | name |
|=================|===========|========|
| 1 | 1 | Small |
| 2 | 1 | Medium |
| 3 | 1 | Large |
此代码根据“表1”中的“sort_order”值对结果进行排序:
$option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_value WHERE option_id = '" . (int)$option_id . "' ORDER BY sort_order ASC");
如何保留显示结果的相同代码,但是按表2中的“名称”对结果进行排序?
答案 0 :(得分:1)
您需要JOIN
option_value_id字段上的两个表:
select ovd.*
from option_value ov
join option_value_description ovd on ov.option_value_id = ovd.option_value_id
where ov.option_id = ?
order by ovd.name
答案 1 :(得分:1)
您可以使用INNER JOIN
查询,这样您就可以加入表格了,然后您就可以按第二个表格的name
列进行排序
$option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_value a LEFT JOIN " . DB_PREFIX . "option_value_description b ON a.option_id = b.option_id AND a.option_value_id = b.option_value_id WHERE a.option_id = '" . (int)$option_id . "' ORDER BY v.name ASC");