gpt4 book ai didi

具有多个 WHERE 的 MySQL 多个 JOIN 子句

转载 作者:行者123 更新时间:2023-11-29 03:52:14 25 4
gpt4 key购买 nike

这是第三次编辑。根据您的所有反馈,我能够使用多个搜索条件生成以下查询。

请注意,这是一个现有系统,预算存在问题,因此我正在尽我所能改进现有查询。您看到的搜索是基于数组手动完成的,没有连接。同样的搜索需要 2-3 分钟来处理,而感谢所有摇滚大师,现在需要 7-8 秒来处理:)

SELECT SQL_CALC_FOUND_ROWS fname, lname, desig, company, region, state, country, add_uid, contacts.`id` as id
FROM contacts
INNER JOIN contact_to_categories ON contact_to_categories.contactid = contacts.id
AND (
contact_to_categories.catid = '2'
)

INNER JOIN contact_professional_details ON contact_professional_details.contact_id = contacts.id
AND (
FIND_IN_SET('1', contact_professional_details.pd_insid)
OR FIND_IN_SET(' 8', contact_professional_details.pd_insid)
OR FIND_IN_SET(' 33', contact_professional_details.pd_insid)
)

AND (
FIND_IN_SET('4', contact_professional_details.pd_secid)
OR FIND_IN_SET('3', contact_professional_details.pd_secid)
OR FIND_IN_SET('5', contact_professional_details.pd_secid)
OR FIND_IN_SET('7', contact_professional_details.pd_secid)
OR FIND_IN_SET('12', contact_professional_details.pd_secid)
OR FIND_IN_SET('11', contact_professional_details.pd_secid)
OR FIND_IN_SET('9', contact_professional_details.pd_secid)
OR FIND_IN_SET('38', contact_professional_details.pd_secid)
OR FIND_IN_SET('35', contact_professional_details.pd_secid)
OR FIND_IN_SET('115', contact_professional_details.pd_secid)
)

INNER JOIN contact_address ON contact_address.contact_id = contacts.id
AND (
contact_address.hmregion IN ('AF', 'EU', 'OC', 'SA')
OR contact_address.hmcountry IN ('Algeria', 'Angola', 'Benin', 'Comoros', 'Andorra', 'Austria', 'Belarus', 'Belgium', 'American Samoa', 'Australia', 'French Polynesia', 'Guam', 'Kiribati', 'Marshall Islands', 'Colombia', 'Ecuador', 'Falkland Islands', 'Guyana', 'Paraguay', 'Peru', 'Laos', 'Malaysia', 'Myanmar', 'Singapore', 'Vietnam')
OR contact_address.hmcity = 'singapore'
)

INNER JOIN contact_offices ON contact_offices.contact_id = contacts.id
AND (
contact_offices.off_region IN ('AF', 'EU', 'OC', 'SA')
OR contact_offices.off_country IN ('Algeria', 'Angola', 'Benin', 'Comoros', 'Andorra', 'Austria', 'Belarus', 'Belgium', 'American Samoa', 'Australia', 'French Polynesia', 'Guam', 'Kiribati', 'Marshall Islands', 'Colombia', 'Ecuador', 'Falkland Islands', 'Guyana', 'Paraguay', 'Peru', 'Laos', 'Malaysia', 'Myanmar', 'Singapore', 'Vietnam')
OR contact_offices.off_city = 'singapore'
)


WHERE 1 AND (
FIND_IN_SET('1', contacts.ins_id)
OR FIND_IN_SET(' 8', contacts.ins_id)
OR FIND_IN_SET(' 33', contacts.ins_id)
)

AND (
FIND_IN_SET('4', contacts.sec_id)
OR FIND_IN_SET('3', contacts.sec_id)
OR FIND_IN_SET('5', contacts.sec_id)
OR FIND_IN_SET('7', contacts.sec_id)
OR FIND_IN_SET('12', contacts.sec_id)
OR FIND_IN_SET('11', contacts.sec_id)
OR FIND_IN_SET('9', contacts.sec_id)
OR FIND_IN_SET('38', contacts.sec_id)
OR FIND_IN_SET('35', contacts.sec_id)
OR FIND_IN_SET('115', contacts.sec_id)
)

AND ( FIND_IN_SET('Tier 1', `vip_tier`) OR FIND_IN_SET('Tier 3', `vip_tier`) )
AND ( FIND_IN_SET('Tier A', `vip_coll_tier`) )
AND ( FIND_IN_SET('Yes', `vip_influencer`) )
AND ( FIND_IN_SET('Contemporary', `vip_class_art_coll`) OR FIND_IN_SET('Modern', `vip_class_art_coll`) OR FIND_IN_SET('Geographic', `vip_class_art_coll`) )
AND ( FIND_IN_SET('Sculpture', `vip_med_art_coll`) OR FIND_IN_SET('Photography', `vip_med_art_coll`) OR FIND_IN_SET('Video', `vip_med_art_coll`) OR FIND_IN_SET('Installation', `vip_med_art_coll`) )
AND ( FIND_IN_SET('Japan', `vip_geo_int`) OR FIND_IN_SET('Korea', `vip_geo_int`) OR FIND_IN_SET('Southeast Asia', `vip_geo_int`) OR FIND_IN_SET('Oceania', `vip_geo_int`) )
AND ( FIND_IN_SET('HNWI', `vip_seniority`) OR FIND_IN_SET('Top Social Leaders', `vip_seniority`) OR FIND_IN_SET('Other Executives', `vip_seniority`) )
AND ( `status` = 'a' )

ORDER BY fname
asc
LIMIT 0, 50

我知道这可以通过将查找设置值移动到单独的表并在联系人主表和值主表之间创建关系表来进一步改进。但正如我所说,预算对这些人来说是个大问题,所以我想这对他们来说不仅高效。

然而,我们欢迎任何进一步的改进想法。

最佳答案

这是在您的查询中给出错误的部分

INNER JOIN contact_professional_details 
ON contact_professional_details.contact_id = contacts.id
AND ( <-- Here
INNER JOIN contact_to_categories
ON contact_to_categories.contactid = contacts.id
AND (
contact_to_categories.catid = '2'
)

将此更改为

INNER JOIN contact_professional_details 
ON contact_professional_details.contact_id = contacts.id

INNER JOIN contact_to_categories
ON contact_to_categories.contactid = contacts.id
AND contact_to_categories.catid = '2'

编辑:您发布的查询一团糟,您确实多次加入了同一个表,并且确实使用了链式 OR 条件而不是 IN 子句。因此,下面是您修改后的查询。

SELECT SQL_CALC_FOUND_ROWS fname, 
lname,
desig,
company,
region,
state,
country,
add_uid,
contacts.`id` as id
FROM contacts

INNER JOIN contact_to_categories
ON contact_to_categories.contactid = contacts.id
AND contact_to_categories.catid = '2'

INNER JOIN contact_professional_details
ON contact_professional_details.contact_id = contacts.id
AND (
FIND_IN_SET('4', contact_professional_details.pd_secid)
OR FIND_IN_SET('3', contact_professional_details.pd_secid)
OR FIND_IN_SET('5', contact_professional_details.pd_secid)
OR FIND_IN_SET('7', contact_professional_details.pd_secid)
OR FIND_IN_SET('12', contact_professional_details.pd_secid)
OR FIND_IN_SET('11', contact_professional_details.pd_secid)
OR FIND_IN_SET('9', contact_professional_details.pd_secid)
OR FIND_IN_SET('38', contact_professional_details.pd_secid)
OR FIND_IN_SET('35', contact_professional_details.pd_secid)
OR FIND_IN_SET('115', contact_professional_details.pd_secid)
)

INNER JOIN contact_address ON contact_address.contact_id = contacts.id
AND
contact_address.hmregion IN ('AF','EU','OC','SA')
AND
contact_address.hmcountry IN ('Algeria',
'Angola',
'Benin',
'Comoros',
'Andorra',
'Austria',
'Belarus',
'Belgium',
'American Samoa',
'Australia',
'French Polynesia',
'Guam',
'Kiribati',
'Marshall Islands',
'Colombia',
'Ecuador',
'Falkland Islands',
'Guyana',
'Paraguay',
'Peru',
'Laos',
'Malaysia',
'Myanmar',
'Singapore',
'Vietnam'
)
AND contact_address.hmcity='singapore'

INNER JOIN contact_offices ON contact_offices.contact_id = contacts.id
AND
contact_offices.off_region IN ('AF','EU','OC','SA')
AND
contact_offices.off_country IN ('Algeria',
'Angola',
'Benin',
'Comoros',
'Andorra',
'Austria',
'Belarus',
'Belgium',
'American Samoa',
'Australia',
'French Polynesia',
'Guam',
'Kiribati',
'Marshall Islands',
'Colombia',
'Ecuador',
'Falkland Islands',
'Guyana',
'Paraguay',
'Peru',
'Laos',
'Malaysia',
'Myanmar',
'Singapore',
'Vietnam'
)
AND contact_offices.off_city='singapore'



WHERE 1 AND (
FIND_IN_SET('1', contacts.ins_id)
OR FIND_IN_SET(' 8', contacts.ins_id)
OR FIND_IN_SET(' 33', contacts.ins_id)
)

AND (
FIND_IN_SET('4', contacts.sec_id)
OR FIND_IN_SET('3', contacts.sec_id)
OR FIND_IN_SET('5', contacts.sec_id)
OR FIND_IN_SET('7', contacts.sec_id)
OR FIND_IN_SET('12', contacts.sec_id)
OR FIND_IN_SET('11', contacts.sec_id)
OR FIND_IN_SET('9', contacts.sec_id)
OR FIND_IN_SET('38', contacts.sec_id)
OR FIND_IN_SET('35', contacts.sec_id)
OR FIND_IN_SET('115', contacts.sec_id)
)

AND (FIND_IN_SET('Tier 1', `vip_tier`) OR FIND_IN_SET('Tier 3', `vip_tier`))
AND (FIND_IN_SET('Tier A', `vip_coll_tier`))
AND (FIND_IN_SET('Yes', `vip_influencer`))
AND (FIND_IN_SET('Contemporary', `vip_class_art_coll`) OR FIND_IN_SET('Modern', `vip_class_art_coll`) OR FIND_IN_SET('Geographic', `vip_class_art_coll`))
AND (FIND_IN_SET('Sculpture', `vip_med_art_coll`) OR FIND_IN_SET('Photography', `vip_med_art_coll`) OR FIND_IN_SET('Video', `vip_med_art_coll`) OR FIND_IN_SET('Installation', `vip_med_art_coll`))
AND (FIND_IN_SET('Japan', `vip_geo_int`) OR FIND_IN_SET('Korea', `vip_geo_int`) OR FIND_IN_SET('Southeast Asia', `vip_geo_int`) OR FIND_IN_SET('Oceania', `vip_geo_int`))
AND (FIND_IN_SET('HNWI', `vip_seniority`) OR FIND_IN_SET('Top Social Leaders', `vip_seniority`) OR FIND_IN_SET('Other Executives', `vip_seniority`))
AND (`status`='a')

ORDER BY fname asc
LIMIT 0,50

关于具有多个 WHERE 的 MySQL 多个 JOIN 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23950759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com