【发布时间】:2017-03-22 02:58:53
【问题描述】:
这是我的文件位置表中的一些数据:
Filelocation_table
+-----+-------------+---------------------+
| id | f_id | f_location |
+-----+-------------+---------------------+
| 1 | 1 | App. Box March 2017 |
| 2 | 2 | App. Box March 2017 |
| 3 | 3 | App. Box March 2017 |
| 4 | 4 | App. Box March 2017 |
| 5 | 5 | App. Box March 2017 |
| 6 | 6 | App. Box March 2017 |
| 7 | 7 | App. Box March 2017 |
| 8 | 8 | App. Box March 2017 |
| 9 | 9 | App. Box March 2017 |
| 10 | 10 | App. Box March 2017 |
| 11 | 11 | App. Box March 2017 |
and so on...
| 294 | 79-EC | EC B1 |
| 295 | 80-EC | EC B1 |
| 296 | 81-EC | EC B1 |
| 297 | 82-EC | EC B1 |
| 298 | 83-EC | EC B1 |
| 299 | 84-EC | EC B1 |
| 300 | 85-EC | EC B1 |
| 301 | 86-EC | EC B1 |
| 302 | 87-EC | EC B1 |
| 303 | 1-processed | Active 1 |
| 304 | 2-processed | Active 1 |
| 305 | 3-processed | PR 1 |
| 306 | 4-processed | PR 1 |
+-----+-------------+---------------------+
我在下面的查询中遇到了问题:
SELECT docu_id.id, /* For checking purposes if it match with the f_id */
filelocation.f_id, /* For checking purposes if it match with the id */
docutype,
f_location,
bpnum,
lastname,
firstname,
middlename,
ponum,
claimtype
FROM docu_id
left join filelocation on
filelocation.f_id = docu_id.id
left join f_dates on
filelocation.f_id = f_dates.f_id
它给出以下结果:
+-----+-------------+-------------+---------------------+------------+--------------+----------------+------------+-------+---------------------------------+
| id | f_id | docutype | f_location | bpnum | lastname | firstname | middlename | ponum | claimtype |
+-----+-------------+-------------+---------------------+------------+--------------+----------------+------------+-------+---------------------------------+
| 1 | 1 | APPLICATION | App. Box March 2017 | xxxxxxxxxx | xxxxxxxxxx | JULIO | - | - | FUNERAL |
| 2 | 2 | APPLICATION | App. Box March 2017 | xxxxxxxxxx | xxxxxxxxxx | ELEUTERIO | | | COMPULSORY LIFE |
| 3 | 3 | APPLICATION | App. Box March 2017 | xxxxxxxxxx | xxxxxxxxxx | ZORAIDA | | | EMPLOYEES COMPENSATION |
/* and so on... */
| 86 | 86-EC | APPLICATION | EC B1 | xxxxxxxxxx | xxxxxxxxxx | DANILO | - | - | REQUEST |
| 87 | 87-EC | APPLICATION | EC B1 | xxxxxxxxxx | xxxxxxxxxx | LIBERATA | - | - | |
| 1 | 1-processed | APPLICATION | Active 1 | xxxxxxxxxx | xxxxxxxxxx | JULIO | - | - | FUNERAL |
| 2 | 2-processed | APPLICATION | Active 1 | xxxxxxxxxx | xxxxxxxxxx | ELEUTERIO | | | COMPULSORY LIFE |
| 3 | 3-processed | APPLICATION | PR 1 | xxxxxxxxxx | xxxxxxxxxx | ZORAIDA | | | EMPLOYEES COMPENSATION |
| 4 | 4-processed | APPLICATION | PR 1 | xxxxxxxxxx | xxxxxxxxxx | FLORA | | | PRE-NEED |
+-----+-------------+-------------+---------------------+------------+--------------+----------------+------------+-------+---------------------------------+
306 rows in set, 19656 warnings (0.05 sec)
f_id '87-EC' 与 id '87' 匹配
这个查询纠正了问题:
SELECT docu_id.id, /* For checking purposes if it match with the f_id */
filelocation.f_id, /* For checking purposes if it match with the id */
docutype,
f_location,
bpnum,
lastname,
firstname,
middlename,
ponum,
claimtype
FROM docu_id
left join filelocation on
concat(filelocation.f_id, 'a') = concat(docu_id.id, 'a')
left join f_dates on
filelocation.f_id = f_dates.f_id
此查询返回预期结果:
+-----+------+-------------+---------------------+------------+--------------+----------------+------------+-------+---------------------------------+
| id | f_id | docutype | f_location | bpnum | lastname | firstname | middlename | ponum | claimtype |
+-----+------+-------------+---------------------+------------+--------------+----------------+------------+-------+---------------------------------+
| 1 | 1 | APPLICATION | App. Box March 2017 | xxxxxxxxxx | xxxxxxxxxx | JULIO | - | - | FUNERAL |
| 2 | 2 | APPLICATION | App. Box March 2017 | xxxxxxxxxx | xxxxxxxxxx | ELEUTERIO | | | COMPULSORY LIFE |
| 3 | 3 | APPLICATION | App. Box March 2017 | xxxxxxxxxx | xxxxxxxxxx | ZORAIDA | | | EMPLOYEES COMPENSATION |
| 4 | 4 | APPLICATION | App. Box March 2017 | xxxxxxxxxx | xxxxxxxxxx | FLORA | | | PRE-NEED |
and so on...
| 214 | 214 | APPLICATION | App. Box March 2017 | xxxxxxxxxx | xxxxxxxxxx | ERNESTO | - | - | SEPARATION |
| 215 | 215 | APPLICATION | App. Box March 2017 | xxxxxxxxxx | xxxxxxxxxx | ROLANDO | | | SEPARATION |
+-----+------+-------------+---------------------+------------+--------------+----------------+------------+-------+---------------------------------+
215 rows in set (0.04 sec)
我不应该使用“-”字符吗? 以及为什么 MySQL 将 '87' 与 '87-EC' 相匹配, 但是当我最后将它与“a”连接时不匹配。 (87a / 87-ECa)?
由于我发布机密数据,我将数字和姓氏替换为 xxxxxxx。
【问题讨论】:
-
86-EC可能在数值上是等价的,所以它们被加入了,但这似乎是 MySQL 过于急切了。通常最好连接两个具有相同类型的列,尤其是那些被正确索引的列。 -
你的意思是,你只需要f_id字段的数字?
-
我有不同的表,每个表都有自己的类型,如 EC、Application、Voucher 和默认表。然后是默认表的位置表,我只使用 id 然后将其插入 f_id(文件 id)中,对于特殊表(EC,VOUCHERS,ETC),我将它们与它们对应的 id 与'-TYPE'(1 -APPLICATION, 1-VOUCHER) in f_id
-
您可以只使用
filelocation.f_id = concat(docu_id.id),因为问题似乎是比较的左侧部分自动转换为数字而不是右侧部分转换为字符串以匹配数据类型。 -
@maraca 我认为反之亦然,f_id 会自动转换为匹配 docu_id.id 谢谢大家的 cmets 和回复。