1

I posted another question which was resolved perfectly however I now need to apply the same code I was given to a different piece of MySQL code.

What I have is

SELECT value, COUNT(*) AS 'num' 
FROM table_c 
WHERE table_c_id IN (9, 17, 25)
GROUP BY value

What I would like to do now is only show the results if they have been entered on the current date?

The current code snippet I have for checking the current date, which works great is (the date is in unixtime format)

( xxxxxxxxxxxxxx and curdate() = date( from_unixtime( b.crm_date_time_column ) ) )

The problem I have with query this is the date column is located in a totally different table, table_a.

How do I write the MySQL to check table_a for the date and apply the existing date SQL I have?

This is a MySQL database.

Any help will be gratefully received! This is way over my head!

4

2 回答 2

2

您需要JOIN首先使用相关列将另一个表放在第一个表上(我假设id在另一个表中与 相关table_c_id)。

正如我在对您之前的问题的回答中所说的那样,您最好在裸日期时间列上进行比较,以便查询保持可搜索(即能够利用索引):

SELECT     a.value
FROM       table_c a
INNER JOIN table_a b ON a.table_c_id = b.id
WHERE      a.table_c_id IN (9,17,25) AND
           b.crm_date_time_column >= UNIX_TIMESTAMP(CURDATE())
GROUP BY   a.value 

这假设crm_date_time_column将永远不会包含未来的时间(例如明天,下个月等),但如果可以,您只需添加:

AND b.crm_date_time_column < UNIX_TIMESTAMP(CURDATE() + INTERVAL 1 DAY)

作为WHERE条款中的另一个条件。

于 2012-07-15T21:38:59.357 回答
1
SELECT   c.value
FROM     table_c c, table_a a
WHERE    c.id IN (9, 17, 25)
 AND     b.crm_date_time_column >= UNIX_TIMESTAMP(CURDATE())
 AND     c.id = a.id
GROUP BY c.value

你可以用这样的查询来做到这一点。它从两个表中选择行,检查它们是否具有相同的 ID 并且当前日期是 table_a 的 crm_date_time_column。我不确定您如何知道系统中哪些行相互链接,因此它会检查它们是否具有相同的 id。

于 2012-07-15T21:31:36.830 回答