Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据另一个字段合并一行_Php_Mysql_Sql_Merge - Fatal编程技术网

Php 根据另一个字段合并一行

Php 根据另一个字段合并一行,php,mysql,sql,merge,Php,Mysql,Sql,Merge,下面是源表和我试图实现的输出 我希望每个_table.id在输出中只出现一次,后续值显示在新列中这将为您提供输出,考虑到您的问题中的表格。使用CTE而不是子查询以增加清晰度 with intials as( SELECT id, another_table.id as another_table_id, value as field_1 FROM main_table WHEN variable.id = 1), finals as( SELECT id, another_table.i

下面是源表和我试图实现的输出


我希望每个_table.id在输出中只出现一次,后续值显示在新列中

这将为您提供输出,考虑到您的问题中的表格。使用CTE而不是子查询以增加清晰度

with intials as(
SELECT id, 
another_table.id as another_table_id,
value as field_1
FROM
main_table
WHEN
variable.id = 1),

finals as(
SELECT id, 
another_table.id as another_table_id,
value as field_1
FROM
main_table
WHEN
variable.id = 2)

SELECT
initials.id,
initials.another_table_id
initials.value field_1,
finals.value field_2
FROM
initials
LEFT OUTER JOIN
finals on initials.another_table_id = finals.another_table_id;

你能举一个你想要的输出的例子吗?你是否试图从表1中获取数据,这样你就可以每一行拥有一个表。id?@Cristian我该怎么做?我不擅长sql..:@链接的示例字段1是我的状态变量\u id=1 inicial status,变量\u id=2->final status,如果我有字段2的值,我必须在屏幕上写下这两个值,所以很抱歉不能更好地解释它,它对我来说太新了。@MarkD nope,我的表另一个表类似于详细信息表,它可能是N行对1关系