0

我正在尝试将 2 个选择语句与不同的列数结合起来。

第一个声明是这样的:

SELECT s.id, s.date_sent, m.sam_subject, m.sam_msg_id, (SELECT
COUNT(id) FROM tbl_something WHERE schedule_id = s.id) AS
total_recipients FROM tbl_something2 AS s INNER JOIN
tbl_something3 AS m ON s.message_id = m.sam_msg_id ORDER BY
s.date_sent DESC

第 2 条声明:

SELECT * FROM sms_something4 WHERE status = '0' ORDER BY id DESC

第一条语句的表输出:

id date_sent sam_subject sam_msg_id total_recipients

1 1372880628 e-Newsletter 2 2

第二次输出:

id | subject | sent | failed | date_sent | data_sent | data_failed | message | sam_uid from | select_members | status | from_no
11 | test    |  2   |  0     |  1372881670 | 639176286411,639224588324 | | | | |                             | 0      | 0

关于如何将这两个陈述结合起来的任何建议?

我的目标输出是

id | subject | sent | failed | date_sent | sam_subject | total_recipients | date_sent for email

sam_msg_id可以忽略。

谢谢你。

4

2 回答 2

1

正如 AJP 所说,您可以这样做:

SELECT s.id, a.subject,a.sent, s.date_sent, m.sam_subject,  
(SELECT COUNT(id) FROM tbl_something WHERE schedule_id = s.id) AS total_recipients 
FROM tbl_something2 AS s 
INNER JOIN tbl_something3 AS m ON s.message_id = m.sam_msg_id 
INNER JOIN
(
SELECT * FROM sms_something4 WHERE status = '0' ORDER BY id DESC
) a on a.subject = m.sam_subject and a.date_sent = s.date_sent
ORDER BY
s.date_sent DESC
于 2013-07-03T20:34:22.647 回答
1

这是您需要具备的基本知识..您可能需要排除故障。根据需要添加列。

SELECT *
FROM (
SELECT s.id, s.date_sent, m.sam_subject, m.sam_msg_id, (SELECT COUNT(id) 
                                                    FROM tbl_something 
                                                    WHERE schedule_id = s.id) AS total_recipients 
FROM tbl_something2 AS s 
INNER JOIN tbl_something3 AS m 
    ON s.message_id = m.sam_msg_id 
) as tbl
INNER JOIN (SELECT * FROM sms_something4 WHERE status = '0') as tbl2
    ON tbl2.subject = tbl.sam_subject
    and tbl.date_sent=tbl2.date_sent
    and tbl.total_Recipients = tbl2.sent+ tbl2.failed
ORDER BY tbl.date_sent DESC
于 2013-07-03T20:35:32.907 回答