如何在 MySQL 中将行显示为列?

发布于 2024-12-11 12:42:11 字数 1161 浏览 0 评论 0原文

伙计们,我有一个名为beneficiaryloans的表,如下

+----+----------------+--------+--------+------+--------+-----------+
| id | beneficiary_id | hfi_id | amount | rate | period | status_id |
+----+----------------+--------+--------+------+--------+-----------+
| 15 |             37 |    116 |    123 |  123 |    123 |         4 |
| 16 |             38 |    117 |    123 |  123 |    123 |         4 |
| 17 |             39 |    116 |    123 |  123 |    123 |         4 |
+----+----------------+--------+--------+------+--------+-----------+

我想根据Hfi_id将其显示如下,

+-----------------+---------------------------+
| beneficiary_id  | hfi_id_116 | hfi_id_117   |
+-----------------+----------------------------
| 37              |  True      |    False     |
| 38              |  False     |    True      |
| 39              |  True      |    False     |
+----------------------------------------------

如何在MySQL中执行此操作?

注意:这里我发布了一些行,并且hfi_ids在这个表中保留标记,如果3 hfi_id那么我需要3列,如果 4 hfi_ids 那么我需要 4 列 等等

Guys I've table called beneficiaryloans as follows

+----+----------------+--------+--------+------+--------+-----------+
| id | beneficiary_id | hfi_id | amount | rate | period | status_id |
+----+----------------+--------+--------+------+--------+-----------+
| 15 |             37 |    116 |    123 |  123 |    123 |         4 |
| 16 |             38 |    117 |    123 |  123 |    123 |         4 |
| 17 |             39 |    116 |    123 |  123 |    123 |         4 |
+----+----------------+--------+--------+------+--------+-----------+

I want to Display It as follows based on Hfi_id

+-----------------+---------------------------+
| beneficiary_id  | hfi_id_116 | hfi_id_117   |
+-----------------+----------------------------
| 37              |  True      |    False     |
| 38              |  False     |    True      |
| 39              |  True      |    False     |
+----------------------------------------------

How to do this in MySQL?

Note: Here I posted some of rows, and hfi_ids are keep stamping in this table, if 3 hfi_id then i need 3 columns, if 4 hfi_ids then i need 4 columns and so on

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

英雄似剑 2024-12-18 12:42:11

试试这个:

SELECT beneficiary_id, 
  CASE hfi_id
      WHEN 116 THEN true
      ELSE false
  END AS hfi_id_116,
  CASE hfi_id
      WHEN 117 THEN true
      ELSE false
  END AS hfi_id_117
FROM your_table

或者如果你需要字符串

SELECT beneficiary_id, 
  CASE hfi_id
      WHEN 116 THEN 'True'
      ELSE 'False'
  END AS hfi_id_116,
  CASE hfi_id
      WHEN 117 THEN 'True'
      ELSE 'False'
  END AS hfi_id_117
FROM your_table

Try this:

SELECT beneficiary_id, 
  CASE hfi_id
      WHEN 116 THEN true
      ELSE false
  END AS hfi_id_116,
  CASE hfi_id
      WHEN 117 THEN true
      ELSE false
  END AS hfi_id_117
FROM your_table

or if you need strings

SELECT beneficiary_id, 
  CASE hfi_id
      WHEN 116 THEN 'True'
      ELSE 'False'
  END AS hfi_id_116,
  CASE hfi_id
      WHEN 117 THEN 'True'
      ELSE 'False'
  END AS hfi_id_117
FROM your_table
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文