- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
示例表,
+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
| 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/
假设你有 Select group_concat(distinct tag.title) as tagTitles group_concat(distinct tag.id) as tagIds ar
我正在建立一个销售鞋子的网上商店。我正在尝试编写一个命令来显示 1 个特定运动鞋的每种尺寸的数量。 这是我的 ER 图 Link to my ER-Diagram can be seen here:
有可能吗?我想要得到的是这样的: +-------------------------------+ | data 1 (another1, another2); | | data 2 (anoth
我正在分析一些查询,并在尝试根据 user_id 从不同的表中提取多个字段时注意到一些有趣的结果。考虑下表: +------------------------+------------------+
不确定标题是否正确解释了情况,但我会尽力在这里解释。 我有一个表,其中有 3 个字段链接到其他表,我想按以下方式对所有行进行分组: item_id, user_id, group_id 1
SELECT CONCAT(`date`,',',`viewcount`) FROM `stat` WHERE `stat`.`id` = 1 AND `channelstat`.`date` B
这是我第一次使用 stackoverflow,..对我温柔点;) 当在 map 表上使用多个 JOIN 时,从 GROUP_CONCAT 获取重复结果几乎没有问题。 解释这个并不容易,但我会尝试: 我
我有以下查询: SELECT files.file_name, files.locked, projects.project_name, group_concat( versions.version,
我想列出所有用户及其对应的用户类别。这是我的表格的简化版本 CREATE TABLE users ( user_id INT NOT NULL AUTO_INCREMENT, user
MYSQL group_concat() 函数默认忽略空列,但不忽略空字符串列。我有一个 mediumtext 类型的字段,而不是 null。当我在该查询上使用 group_concat 函数时,生成
是否可以使用 Mysql 获取前 n(比如一列的 10 行)行的逗号分隔值? 我有一个查询要获取大于 CURDATE() 的数据。它将返回超过 100 行的结果。我想要的是,GROUP_CONCAT
我刚刚了解到 group_concat和 order by , group by ,现在我正在尝试将它们应用于查询谋杀案。即,到下面的这个以获取所有参与者和目标。 SELECT DISTINCT ?i
在 BigQuery 中创建了以下查询: SELECT date, userId, SUM(totals.visits) totalvisits, GROUP_CONCAT(devic
这是我的 SQL 操作: select `id_ifc_espaces`,GROUP_CONCAT(DISTINCT `nom_espace`) as nom_espace ,GROUP_CONC
我使用 GROUP_CONCAT 来获得由逗号分割的结果( , ),但是当我看到时, GRUP_CONCAT仅返回 205 拆分的数字,但在数据库中有 2448 结果(不同 aid )。这是我的查询:
我想在 table A 上做一个 SELECT,它有: table_a: ╔══════════╦════════╗ ║ GROUP_ID ║ NAME ║ ╠══════════╬═══════
我想根据messageid group_concate image_id 和small_pic_path 路径。 但是 group concate 显示所有 image_id 和第一个 message
我有 table id | industry_id | type | key 1 1 0 word1 2 1 1 wor
我的查询是这样的 SELECT * FROM semesters WHERE student_id = 434 AND marks_id <= 576 AND semester_id <= 23
在 mysql 中是否有 GROUP_CONCAT 的替代方案。由于 GROUP_CONCAN 的最大长度约束是 1024,我需要一个替代方案。我不能更改 GROUP_CONCAN_MAX_LENGT
我是一名优秀的程序员,十分优秀!