MySQL - 具有两个字段的 LEFT JOIN
MySQL - LEFT JOIN with two fields
我有两个 table:
invoices
discount_vouchers
我在invoice
table,有两个字段:
voucher_id
author_voucher_ids
voucher_id是discount_voucherstable的直接id。
作者_voucher_ids 是来自 discount_vouchers table.
的逗号分隔 ID 列表
是否可以 LEFT JOIN
发票和 discount_vouchers table 这两个字段?
我试过:
LEFT JOIN discount_vouchers ON invoices.discount_voucher_id = discount_vouchers.id OR discount_vouchers.id IN (author_voucher_ids)
但这非常慢,我需要终止进程。
我知道 table 发票的设计方式有误,但我正在寻找无需触及现有 table 结构的解决方案。
谢谢。
您可以使用FIND_IN_SETMySQL函数:
LEFT JOIN discount_vouchers ON invoices.discount_voucher_id = discount_vouchers.id
OR FIND_IN_SET(discount_vouchers.id, author_voucher_ids) <> 0
我有两个 table:
invoices
discount_vouchers
我在invoice
table,有两个字段:
voucher_id
author_voucher_ids
voucher_id是discount_voucherstable的直接id。
作者_voucher_ids 是来自 discount_vouchers table.
的逗号分隔 ID 列表是否可以 LEFT JOIN
发票和 discount_vouchers table 这两个字段?
我试过:
LEFT JOIN discount_vouchers ON invoices.discount_voucher_id = discount_vouchers.id OR discount_vouchers.id IN (author_voucher_ids)
但这非常慢,我需要终止进程。
我知道 table 发票的设计方式有误,但我正在寻找无需触及现有 table 结构的解决方案。
谢谢。
您可以使用FIND_IN_SETMySQL函数:
LEFT JOIN discount_vouchers ON invoices.discount_voucher_id = discount_vouchers.id
OR FIND_IN_SET(discount_vouchers.id, author_voucher_ids) <> 0