php - MySQL GROUP_CONCAT & PHP 解析

标签 php mysql

示例表,

+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
| book_id | book_name                           | isbn_no     | cate_id | aut_id | pub_id | dt_of_pub  | pub_lang | no_page | book_price |
+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
| BK001   | Introduction to Electrodynamics     | 0000979001  | CA001   | AUT001 | P003   | 2001-05-08 | English  |     201 |      85.00 |
| BK002   | Understanding of Steel Construction | 0000979002  | CA002   | AUT002 | P001   | 2003-07-15 | English  |     300 |     105.50 |
| BK003   | Guide to Networking                 | 0000979003  | CA003   | AUT003 | P002   | 2002-09-10 | Hindi    |     510 |     200.00 |
| BK004   | Transfer  of Heat and Mass          | 0000979004  | CA002   | AUT004 | P004   | 2004-02-16 | English  |     600 |     250.00 |
| BK005   | Conceptual Physics                  | 0000979005  | CA001   | AUT005 | P006   | 2003-07-16 | NULL     |     345 |     145.00 |
| BK006   | Fundamentals of Heat                | 0000979006  | CA001   | AUT006 | P005   | 2003-08-10 | German   |     247 |     112.00 |
| BK007   | Advanced 3d Graphics                | 0000979007  | CA003   | AUT007 | P002   | 2004-02-16 | Hindi    |     165 |      56.00 |
| BK008   | Human Anatomy                       | 0000979008  | CA005   | AUT008 | P006   | 2001-05-17 | German   |      88 |      50.50 |
| BK009   | Mental Health Nursing               | 0000979009  | CA005   | AUT009 | P007   | 2004-02-10 | English  |     350 |     145.00 |
| BK010   | Fundamentals of Thermodynamics      | 0000979010  | CA002   | AUT010 | P007   | 2002-10-14 | English  |     400 |     225.00 |
| BK011   | The Experimental Analysis of Cat    | 0000979011  | CA004   | AUT011 | P005   | 2007-06-09 | French   |     225 |      95.00 |
| BK012   | The Nature  of World                | 0000979012  | CA004   | AUT005 | P008   | 2005-12-20 | English  |     350 |      88.00 |
| BK013   | Environment a Sustainable Future    | 0000979013  | CA004   | AUT012 | P001   | 2003-10-27 | German   |     165 |     100.00 |
| BK014   | Concepts in Health                  | 0000979014  | CA005   | AUT013 | P004   | 2001-08-25 | NULL     |     320 |     180.00 |
| BK015   | Anatomy & Physiology                | 0000979015  | CA005   | AUT014 | P008   | 2000-10-10 | Hindi    |     225 |     135.00 |
| BK016   | Networks and Telecommunications     | 00009790_16 | CA003   | AUT015 | P003   | 2002-01-01 | French   |      95 |      45.00 |
+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+

输出

mysql> SELECT pub_id,GROUP_CONCAT(CATE_ID)
    -> FROM book_mast
    -> GROUP BY pub_id;
+--------+-----------------------+
| pub_id | GROUP_CONCAT(CATE_ID) |
+--------+-----------------------+
| P001   | CA002,CA004           | 
| P002   | CA003,CA003           | 
| P003   | CA001,CA003           | 
| P004   | CA005,CA002           | 
| P005   | CA001,CA004           | 
| P006   | CA005,CA001           | 
| P007   | CA005,CA002           | 
| P008   | CA005,CA004           | 
+--------+-----------------------+
8 rows in set (0.02 sec)

我们可以用 PHP 来做到这一点,

$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);  
foreach($dbh->query('SELECT pub_id,GROUP_CONCAT(cate_id)  
FROM book_mast  
GROUP BY pub_id') as $row) {  
  //Use $row['GROUP_CONCAT(cate_id)'] 
}  

现在,如果我想解析有关 cate_id = Category ID 的更多详细信息,我是否应该在此 foreach 循环内执行另一个查询?因为我得到了类别的 ID。

最佳答案

我认为使用两个查询并不是一种好的做法,我们可以使用联接查询从多个表中获取数据,或者通过创建表名别名,如果直接使用一个查询会更好。

注意:sql内部查询应该有不同的表名,如下所示

SELECT * FROM wp_posts where ID in(select ID from abc)

对于同一个表,您可以使用php条件进行动态查询。

关于php - MySQL GROUP_CONCAT & PHP 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34313533/

相关文章:

php - 查询数据库和 session

MySQL MIN/MAX 所有行

php - 如何从php中的按钮数组中提取特定的id号?

php mysql 问题

javascript - php 中的两个操作两个提交按钮?

javascript - 如何使用 Javascript 和 PHP 同时继续两种类型的循环

php - mysql如何进行ORDER BY?

Mysql - 选择用户在其个人距离范围内的纬度/经度位置记录

php - 将 MySQL 查询转换为 CI 语法

php - 我可以在 php 中查询这个数组以产生结果还是应该在 MySQL 中完成?