Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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
Mysql 从sql获取列名_Mysql_Sql - Fatal编程技术网

Mysql 从sql获取列名

Mysql 从sql获取列名,mysql,sql,Mysql,Sql,我有这张桌子 id | apple | banana | coconut | pear| code | Class | 1 1 1 1 0 101 B 2 0 0 1 1 102 C 3 1 0 0 1 103 B 我现在有这个问题 select tree from ((select id,

我有这张桌子

id | apple | banana | coconut | pear| code | Class |
1     1       1       1           0    101      B
2     0       0        1          1    102      C
3     1       0        0          1    103      B
我现在有这个问题

select tree
from ((select id, 'apple' as tree
       from trees
       where apple = 1
      ) union all
      (select id, 'banana' as tree
       from trees
       where banana = 1
      ) union all
      (select id, 'coconut' as tree
       from trees
       where coconut = 1
      ) union all
      (select id, 'pear' as tree
       from trees
       where pear = 1
      )
     ) t
where id = 1;
这给了我

apple
banana 
coconut
但我想做的是使用
code
之类的方法查询它
其中code='101'和id=1
,并用于显示
因为输出应该是

   class  |fruits
     B      apple 
            banana  
            coconut
这样的事情是可能的


如果您需要更多说明,请告诉我

您可以在不需要值的select for union中添加所需的列并为null

select tree
from ((select id, 'apple' as tree, code, class
       from trees
       where apple = 1
      ) union all
      (select id, 'banana' as tree, null, null
       from trees
       where banana = 1
      ) union all
      (select id, 'coconut' as tree, null,  null
       from trees
       where coconut = 1
      ) union all
      (select id, 'pear' as tree, null, null
       from trees
       where pear = 1
      )
     ) t
where id = 1;

这样,所有select都具有相同的列数和匹配的数据类型

您可以在不需要值的select for union中添加所需的列和null

select tree
from ((select id, 'apple' as tree, code, class
       from trees
       where apple = 1
      ) union all
      (select id, 'banana' as tree, null, null
       from trees
       where banana = 1
      ) union all
      (select id, 'coconut' as tree, null,  null
       from trees
       where coconut = 1
      ) union all
      (select id, 'pear' as tree, null, null
       from trees
       where pear = 1
      )
     ) t
where id = 1;

通过这种方式,所有select都具有相同的列数和匹配的数据类型,其中code='101'和id=1…是否
id
不唯一?因为如果是,那么id=1的
将执行此任务。但是,如果您实际上只想通过代码进行查询,那么就不要使用ID部分。现在还不清楚你到底想实现什么逻辑。此外,您的数据可能看起来是非规范化的-当您需要添加其他类型的水果时会发生什么?最好使用一个“类”表,该表只包含代码和类列,然后是一个“水果类”表,该表包含“类”表中的ID和水果名称(或者更好,是“水果”表中水果的ID),这样可以支持每个“类”中任意多个不同的水果,添加新的水果只是向表中添加新行,而不是更改模式和更改所有查询。任何时候你都发现自己很难把事情的名称编码成列或查询子句,那么现在是时候考虑一下是否可以对模式进行合理化。我觉得这是一个我已经在这里看到过的几次作业问题。wrong@o3203823,你说得对,这是你几天前的另一篇文章
其中code='101'和id=1
…是否
id
不唯一?因为如果是,那么id=1的
将执行此任务。但是,如果您实际上只想通过代码进行查询,那么就不要使用ID部分。现在还不清楚你到底想实现什么逻辑。此外,您的数据可能看起来是非规范化的-当您需要添加其他类型的水果时会发生什么?最好使用一个“类”表,该表只包含代码和类列,然后是一个“水果类”表,该表包含“类”表中的ID和水果名称(或者更好,是“水果”表中水果的ID),这样可以支持每个“类”中任意多个不同的水果,添加新的水果只是向表中添加新行,而不是更改模式和更改所有查询。任何时候你都发现自己很难把事情的名称编码成列或查询子句,那么现在是时候考虑一下是否可以对模式进行合理化。我觉得这是一个我已经在这里看到过的几次作业问题。wrong@o3203823,你说得对,这是你几天前的另一篇文章。