【问题标题】:Can I use one table field value as second table field name in mysql select query?我可以在 mysql 选择查询中使用一个表字段值作为第二个表字段名称吗?
【发布时间】:2016-02-07 07:42:24
【问题描述】:

场景:

Table1 的字段名称命名为 testtable

id、名称、大小、宽度、高度

Table2的字段名称命名为errortable

id,desc,field1,field2,运算符

错误表的值

+----+-------------------------------------+--------+--------+----------+
| id |                desc                 | field1 | field2 | operator |
+----+-------------------------------------+--------+--------+----------+
|  1 | size should not greater than width  | size   | width  | >        |
|  2 | size should not greater than height | size   | height | >        |
|  3 | with should be equal to height      | width  | height | <>       |
+----+-------------------------------------+--------+--------+----------+

现在我想从 testtable 中检查:

  1. 计算大小>宽度的所有记录

  2. 计算大小>高度的所有记录

  3. 计算宽度 高度的所有记录。

所需输出

+-------------------------------------+-------+
|              errorname              | count |
+-------------------------------------+-------+
| size should not greater than width  |     6 |
| size should not greater than height |     2 |
| with should be equal to height      |     3 |
+-------------------------------------+-------+

这样可以吗?

当前查询:

select desc,(select count(*) as "Total Errors" from testtable where errortable.field1 errortable.operator errortable.field2 ) from errortable group by id

【问题讨论】:

  • MySQL 还是 Postgeslq?不要标记未涉及的产品...
  • 任何人,我都需要
  • 之所以称它们为关系数据库,是因为它们都是关于表之间的关系,而这两个表之间没有关系
  • 关系是不可能的,我有一个错误列表,我想检查另一个表中是否存在许多错误,就是这样,
  • 不,因为您正在尝试使用字段值作为字段名称,而我想不出在 MySQL 中这样做的方法。

标签: php sql database postgresql codeigniter


【解决方案1】:

感谢所有评论的人,我在你们的 cmets 中得到了我的答案,并通过处理 php 中的一些代码来实现这个场景。

【讨论】:

    猜你喜欢
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2013-07-16
    相关资源
    最近更新 更多