使用if语句的mysql concat

时间:2019-07-18 06:37:47

标签: mysql sql concat

我正在为jsdatatables显示生成数据-有四个站点,我需要将每个站点的数据放入列中。大多数列只包含一个数字,但有时也有注释,我需要将它们添加到列中。

我目前无法使用的示例在这里

SELECT a.drug as drug, 
concat_ws(' ',SUM(IF(a.idSite=1, IF(a.expiry > now(),quantity,0), 0)),a.notes) AS 'col1', 
concat_ws(' ',SUM(IF(a.idSite=2, IF(a.expiry > now(),quantity,0), 0)),a.notes) AS 'col2',1,2  
FROM items a   
WHERE a.deleted_at IS NULL  
Group By drug,notes 

该数据当前包含col1的数据,而没有col2的数据

这给了我-请注意,注释出现在col1和2中,而不仅仅是1

+----------------------------------------------------------------------------------------------------------------------------------+-------------------+-------------------+---+---+
| drug                                                                                                                             | col1              | col2              | 1 | 2 |
+----------------------------------------------------------------------------------------------------------------------------------+-------------------+-------------------+---+---+
| (c) Absolute Ethyl Alcohol 98% i.v. Inj 1 ml                                                                                     | 0 n/a             | 0 n/a             | 1 | 2 |
| (c) Diazepam 10mg/2ml Injection                                                                                                  | 1 to be requested | 0 to be requested | 1 | 2 |
| (r) Fomepizole 1 g/ml (1.5ml) i.v. Solution (preservative Free)                                                                  | 8 n/a             | 0 n/a             | 1 | 2 |
| Acetylcysteine 20%                                                                                                               | 70                | 0                 | 1 | 2 |
| Activated Charcoal                                                                                                               | 30                | 0                 | 1 | 2 |
| Atropine 0.1mg/ml Pre Loaded Syringe                                                                                             | 100               | 0                 | 1 | 2 |
| Calcium Disodium Edetate 200mg/ml                                                                                                | 0                 | 0                 | 1 | 2 |
| Calcium gluconate 10%                                                                                                            | 20                | 0                 | 1 | 2 |
| Cyanide Kit Sodium Nitrite 300mg/10 ml + Sodium Thiosulfate 12.5 g/50 ml + Amyl Nitrite 0.3 ml Or Hydroxocobalamin Hydrochloride | 0 Non Formulary   | 0 Non Formulary   | 1 | 2 |

预期产量

+----------------------------------------------------------------------------------------------------------------------------------+-------------------+-------------------+---+---+
| drug                                                                                                                             | col1              | col2              | 1 | 2 |
+----------------------------------------------------------------------------------------------------------------------------------+-------------------+-------------------+---+---+
| (c) Absolute Ethyl Alcohol 98% i.v. Inj 1 ml                                                                                     | 0 n/a             | 0                 | 1 | 2 |
| (c) Diazepam 10mg/2ml Injection                                                                                                  | 1 to be requested | 0                 | 1 | 2 |
| (r) Fomepizole 1 g/ml (1.5ml) i.v. Solution (preservative Free)                                                                  | 8 n/a             | 0                 | 1 | 2 |
| Acetylcysteine 20%                                                                                                               | 70                | 0                 | 1 | 2 |
| Activated Charcoal                                                                                                               | 30                | 0                 | 1 | 2 |
| Atropine 0.1mg/ml Pre Loaded Syringe                                                                                             | 100               | 0                 | 1 | 2 |
| Calcium Disodium Edetate 200mg/ml                                                                                                | 0                 | 0                 | 1 | 2 |
| Calcium gluconate 10%                                                                                                            | 20                | 0                 | 1 | 2 |
| Cyanide Kit Sodium Nitrite 300mg/10 ml + Sodium Thiosulfate 12.5 g/50 ml + Amyl Nitrite 0.3 ml Or Hydroxocobalamin Hydrochloride | 0 Non Formulary   | 0        | 1 | 2 |

项目表

+-----+------------+----------------------------------------------------------------------------------------------------------------------------------+----------+------------+--------+----------+-----------------+
| id  | deleted_at | drug                                                                                                                             | quantity | expiry     | idSite | barcode  | notes           |
+-----+------------+----------------------------------------------------------------------------------------------------------------------------------+----------+------------+--------+----------+-----------------+
| 739 | NULL       | (c) Absolute Ethyl Alcohol 98% i.v. Inj 1 ml                                                                                     |        0 | 2021-07-18 |      1 | 41125225 | n/a             |
| 738 | NULL       | (c) Diazepam 10mg/2ml Injection                                                                                                  |        1 | 2020-08-01 |      1 | 41061007 | to be requested |
| 740 | NULL       | (r) Fomepizole 1 g/ml (1.5ml) i.v. Solution (preservative Free)                                                                  |        8 | 2021-07-01 |      1 | 41992542 | n/a             |
| 708 | NULL       | Acetylcysteine 20%                                                                                                               |       70 | 2019-11-01 |      1 | 41121031 | NULL            |
| 709 | NULL       | Activated Charcoal                                                                                                               |       30 | 2020-01-01 |      1 | 41125067 | NULL            |
| 710 | NULL       | Atropine 0.1mg/ml Pre Loaded Syringe                                                                                             |      100 | 2021-07-01 |      1 | 41061203 | NULL            |
| 711 | NULL       | Calcium Disodium Edetate 200mg/ml                                                                                                |       30 | 2019-06-01 |      1 | 41121009 | NULL            |
| 712 | NULL       | Calcium gluconate 10%                                                                                                            |       20 | 2020-11-01 |      1 | 41091003 | NULL            |
| 736 | NULL       | Cyanide Kit Sodium Nitrite 300mg/10 ml + Sodium Thiosulfate 12.5 g/50 ml + Amyl Nitrite 0.3 ml Or Hydroxocobalamin Hydrochloride |        0 | 2021-07-18 |      1 | 41002215 | Non Formulary   |

0 个答案:

没有答案