mysql - 对不同列进行多个 mysql 查询

标签 mysql sql

如何在不同的列上显示多个 mysql 查询的输出,下面的代码不起作用并且包含 3 个查询,但是我能够让它工作于 2 个查询,请参见下面

带有 3 个子查询的 mysql 查询:

Select (
SELECT (
        SELECT count(s2.centerpoint_stream_stable)
        FROM status_log s2
        WHERE s2.centerpoint_stream_stable = 'Disconnected/ Reconnected to Stream'
        AND s2.date > date_sub(NOW(), INTERVAL 1 minute)  
        ) AS centerpoint_stream_no_disconnects,

count(centerpoint_stream_stable) from status_log where  centerpoint_stream_stable = 'Disconnected/ Reconnected to Stream' and  date > date_sub(now(), interval 6 hour) ) AS a,b,       



s.date, s.website_online, s.icecast_source_online, s.icecast_source_ip,
s.icecast_no_listeners, s.centerpoint_online, s.centerpoint_connection,
s.centerpoint_stream_stable, s.centerpoint_stream_status,  s.horsleypark_online,
s.horsleypark_connection, s.horsleypark_stream_stable, s.horsleypark_stream_status,
s.local_primary_internet_online, s.local_primary_internet_ping,
s.local_primary_instreamer_online, s.local_secondary_internet_online,
s.local_secondary_internet_ping, s.local_secondary_instreamer_online, s.system_ok
FROM status_log s
WHERE id = (select max(id) from status_log)

mysql 查询 2(工作):

SELECT (
        SELECT count(s2.centerpoint_stream_stable)
        FROM status_log s2
        WHERE s2.centerpoint_stream_stable = 'Disconnected/ Reconnected to Stream'
        AND s2.date > date_sub(NOW(), INTERVAL 1 minute)  
        ) AS centerpoint_stream_no_disconnects,


s.date, s.website_online, s.icecast_source_online, s.icecast_source_ip,
s.icecast_no_listeners, s.centerpoint_online, s.centerpoint_connection,
s.centerpoint_stream_stable, s.centerpoint_stream_status,  s.horsleypark_online,
s.horsleypark_connection, s.horsleypark_stream_stable, s.horsleypark_stream_status,
s.local_primary_internet_online, s.local_primary_internet_ping,
s.local_primary_instreamer_online, s.local_secondary_internet_online,
s.local_secondary_internet_ping, s.local_secondary_instreamer_online, s.system_ok
FROM status_log s
WHERE id = (select max(id) from status_log)

输出:

+-----------------------------------+---------------------+----------------+-----------------------+-------------------+----------------------+--------------------+------------------------+---------------------------+---------------------------+--------------------+------------------------+---------------------------+---------------------------+-------------------------------+-----------------------------+---------------------------------+---------------------------------+-------------------------------+-----------------------------------+-----------+
| centerpoint_stream_no_disconnects | date                | website_online | icecast_source_online | icecast_source_ip | icecast_no_listeners | centerpoint_online | centerpoint_connection | centerpoint_stream_stable | centerpoint_stream_status | horsleypark_online | horsleypark_connection | horsleypark_stream_stable | horsleypark_stream_status | local_primary_internet_online | local_primary_internet_ping | local_primary_instreamer_online | local_secondary_internet_online | local_secondary_internet_ping | local_secondary_instreamer_online | system_ok |
+-----------------------------------+---------------------+----------------+-----------------------+-------------------+----------------------+--------------------+------------------------+---------------------------+---------------------------+--------------------+------------------------+---------------------------+---------------------------+-------------------------------+-----------------------------+---------------------------------+---------------------------------+-------------------------------+-----------------------------------+-----------+
|                                 0 | 2018-01-14 10:54:40 | Online         | Online                | 60.241.175.9      | 21                   | Online             | Main                   | system ok                 | PLAYING                   | Online             | Main                   | system ok                 | PLAYING                   | Online                        | 26.556                      | Online                          | Online                          | 22.625                        | Online                            | Online    |
+-----------------------------------+---------------------+----------------+-----------------------+-------------------+----------------------+--------------------+------------------------+---------------------------+---------------------------+--------------------+------------------------+---------------------------+---------------------------+-------------------------------+-----------------------------+---------------------------------+---------------------------------+-------------------------------+-----------------------------------+-----------+

最佳答案

select * from
    (select count(centerpoint_stream_stable) from status_log where  centerpoint_stream_stable = 'Disconnected/ Reconnected to Stream' and  date > date_sub(now(), interval 6 hour)) as T1
    ,
    (select count(centerpoint_stream_stable) from status_log where  centerpoint_stream_stable = 'Disconnected/ Reconnected to Stream' and  date > date_sub(now(), interval 6 hour) ) as T2
    ,
    (select  s.date, s.website_online, s.icecast_source_online, s.icecast_source_ip,
       s.icecast_no_listeners, s.centerpoint_online, s.centerpoint_connection,
       s.centerpoint_stream_stable, s.centerpoint_stream_status,  s.horsleypark_online,
       s.horsleypark_connection, s.horsleypark_stream_stable, s.horsleypark_stream_status,
       s.local_primary_internet_online, s.local_primary_internet_ping,
       s.local_primary_instreamer_online, s.local_secondary_internet_online,
       s.local_secondary_internet_ping, s.local_secondary_instreamer_online, s.system_ok
       FROM status_log s
        WHERE id = (select max(id) from status_log))  as T3

关于mysql - 对不同列进行多个 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48245456/

相关文章:

sql - oracle中的真实表空间大小

用于获取表名、 View 和存储过程的 SQL 语句,按模式排序

sql - 有没有人将 .NET WinForms 应用程序部署到日本

php - 如何使用Union优化Mysql Select查询?

MySQL 用变量替换字符串

大量 varchar 字段的 mysql 表规范

PHP - SELECT * FROM ...但首先查看数据库中的最后一个元素

php - SQL帮助,无法通过连接获得所需的输出

mysql - 对于员工表中的每个员工,查找层次结构中直接和间接老板的数量

mysql - 简单 SQL 连接的问题