我正在尝试从两个不同的数据库对两个表进行内连接。
SELECT
trans.user_id
FROM
(SELECT
user_id, timestamp AS transection
FROM
`Abcd_Transactions`.`Transaction_Archives`) AS trans
INNER JOIN
(SELECT
userid
FROM
Abcd_QNA.qa_posts
WHERE
type = 'Q') AS ques ON trans.user_id = ques.userid
但是在上面trans.user_id是STRING但是ques.userid是INTEGER。所以它给了我意想不到的结果。为了将ques.userid转换为char。我用过。
convert(ques.userid, char)
但是,这需要无限的时间。
我发现许多链接到这样的问题,并声称有解决方案,但我没有得到适当的答案。
答案 0 :(得分:0)
尝试这个,是的查询将采用因为使用find_in_set函数不会使用索引,因为不同的数据类型 -
SELECT
trans.user_id
FROM
(SELECT
user_id, TIMESTAMP AS transection
FROM
`Abcd_Transactions`.`Transaction_Archives`) AS trans
INNER JOIN
(SELECT
userid
FROM
Abcd_QNA.qa_posts
WHERE
TYPE = 'Q') AS ques ON FIND_IN_SET(ques.userid,trans.user_id)
即使是下面的查询也可以很快 -
SELECT trans.user_id, trans.TIMESTAMP AS transection, ques.
FROM `Abcd_Transactions`.`Transaction_Archives` AS trans
JOIN Abcd_QNA.qa_posts AS ques ON FIND_IN_SET(ques.userid,trans.user_id)
WHERE ques.type='Q';