我有两个表,我需要一个(左/右/内)加入两个日期字段。MySQL:问题与JOIN ON时间戳和日期
transactions
表具有时间戳数据类型。 (Adminer的方式是我可以用“人格”来读取数据,而不是数量,就像你注意到的那样)。
另一个calendar
表的数据类型为date
。
我要疯了试图cast
/convert
第一个timestamp
到date
没有成功。 JOIN
根本不是JOIN
,就像两个数据是不同的类型。
SELECT *
FROM transactions
RIGHT JOIN calendar ON calendar.day_date = DATE(transactions.dateTransaction)
我在calendar
transactions
和100 2倍的值,所以我很期待100点的记录 - 98 NULL和2填充。
当然,这不也改变:
SELECT *
FROM transactions
LEFT JOIN calendar ON calendar.day_date = DATE(transactions.dateTransaction)
,或者还带有双DATE
:
SELECT *
FROM transactions
LEFT JOIN calendar ON DATE(calendar.day_date) = DATE(transactions.dateTransaction)
或者也:
SELECT *
FROM calendar c
RIGHT JOIN transactions t ON c.day_date = DATE(t.dateTransaction)
SELECT *
FROM calendar c
LEFT JOIN transactions t ON c.day_date = DATE(t.dateTransaction)
我只获得2笔交易记录,我预计约100(100日历)。或者也可以使用FROM_UNIXTIME
。
transactions
表模式:
CREATE TABLE `transactions`
( `idTransactions` int(11) NOT NULL AUTO_INCREMENT,
`dateTransaction` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`idTransactions`),
KEY `index_dateTransaction` (`dateTransaction`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=155731 DEFAULT CHARSET=utf8
calendar
表模式
CREATE TABLE `calendar`
( `day_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
*我要疯投/第一时间戳记转换为数据*什么你的表列类型?首先发布您的架构 – Alex
您是否尝试将calendar.day_date投射到日期?又名'DATE(calendar.day_date)'这也会有问题。 timestamp字段在特定日期有多个记录时会发生什么?它会给你多条记录 –
@Alex添加了模式 – sineverba