mysql - 如何在 PHP 中从单个表连接 Mysql 中的多个条件的多个查询?

标签 mysql sql pivot

我在 MySQL 中有一个表

------------------------------
| ID | Subject | Marks | Term |
-------------------------------
| 1  | English | 45    | 1    |
| 2  | Hindi   | 34    | 1    |
| 3  | English | 54    | 2    |
| 4  | Hindi   | 33    | 2    |
------------------------------

我想要的输出如下:

--------------------------------------
| Subject | Marks_Term1 | Marks_Term2 |
--------------------------------------
| English |  45         | 54          |
|  Hindi  |  34         | 33          |
--------------------------------------

请帮助我编写代码,我们将不胜感激。

SELECT marks_entry.subject_id, marks_entry.main_exam, marks_entry.main_exam
FROM marks_entry 
WHERE marks_entry.term_id = 1 
 LEFT JOIN SELECT marks_entry.subject_id, marks_entry.main_exam, marks_entry.main_exam FROM marks_entry WHERE marks_entry.term_id = 2

最佳答案

试试这个,

SELECT A.Subject, A.Marks AS Marks_Term1, B.Marks AS Marks_Term2
FROM {tableName} A, {tableName} B
WHERE A.Subject = B.Subject

关于mysql - 如何在 PHP 中从单个表连接 Mysql 中的多个条件的多个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59220925/

相关文章:

php - 在 PHPSpreadsheet 上循环后添加单元格

mysql - 德鲁帕尔 6 : using too many Views module causing site to go down cos of too many mysql connection

mysql - 如何在jsp页面显示mysql动态查询

php - 循环表格以在行中显示结果

MySQL 与 DB2 RDBMS

sql - mysql:导出单行,但具有所有依赖项

mysql - 将包含日期的 Varchar 转换为 DATETIME

sql - Coldfusion/sql ASC 顺序,末尾有空格

mysql - 如何像excel中的pivot一样显示mysql中的数据

sql-server - SQL Server 08 - 枢轴 - 更改列名