我正在尝试构建一个查询,它将构建如下所示的输出。
+-----------------+--------------+
| Name | patient appt |
+-----------------+--------------+
| Dharmick Ketan | 75 |
| See Ka Wai | 45 |
| Totoritis Susan | 25 |
| Seay Duval | 147 |
+-----------------+--------------+
我得到的输出是这样的
+-----------------+--------------+
| Name | patient appt |
+-----------------+--------------+
| Dharmick Ketan | patient appt |
| See Ka Wai | patient appt |
| Totoritis Susan | patient appt |
| Seay Duval | patient appt |
+-----------------+--------------+
我是按照这里的说明来的,https://www.mysqltutorial.org/mysql-subquery/
我的问题是
mysql> select concat(p.lname,' ' , p.fname) as 'Name',
-> 'patient appt'
-> from patient_data as p
-> where
-> p.financial_review_exp>'2019-07-01'
-> and
-> 'patient appt' = ( select count(*) from openemr_postcalendar_events e
-> where e.pc_pid = p.pid );
我所期望的是使用嵌套select语句中的数据填充别名'patient appt‘。我认为这是可行的,因为我正在尝试生成多个列,这些列将由子查询填充。所有列都是日程表中约会的计数。
那么,'patient appt‘是否需要是patient_data表中的一列呢?如果是这样,是否有其他方法可以生成所需的列数据?
发布于 2020-11-06 18:52:43
将相关子查询直接放在select
子句中:
select concat(p.lname,' ' , p.fname) as name,
(select count(*) from openemr_postcalendar_events as e where e.pc_pid = p.pid) as patient_appt
from patient_data as p
where p.financial_review_exp > '2019-07-01'
注意:不要对列别名使用单引号!它们仅适用于文字字符串。在MySQL中,您可以使用反引号来引用标识符-但更好的做法是使用不需要引号的标识符,因此您不必担心这一点。
https://stackoverflow.com/questions/64720092
复制相似问题