简体   繁体   English

将三个 SQL 查询合并为一个

[英]Combining three SQL queries into one

I got working code from three queries but I would like to combine them into one or two.我从三个查询中获得了工作代码,但我想将它们组合成一两个。 Basically I am checking if a provided phone number exists in table contacts or leads as well as if it exists as a secondary number in customfieldsvalues (not all leads have a customfield value though).基本上,我正在检查提供的电话号码是否存在于表格联系人或潜在客户中,以及它是否作为辅助号码存在于 customfieldsvalues 中(但并非所有潜在客户都有自定义字段值)。 I am using a CRM system based on CodeIgniter.我正在使用基于 CodeIgniter 的 CRM 系统。

What I want to do (non-correct/hypothetical query):我想要做什么(不正确/假设的查询):

SELECT * FROM contacts OR leads WHERE phonenumber = replace(X, '-', '')
OR leads.id = customvaluefields.relid AND cfields.fieldid = 41 AND cfields.value = X

Tables

table : contacts
+-------+----------------+----------------+
|   id  |   firstname    |  phonenumber   |
+-------+----------------+----------------+
|   1   |      John      |   214-444-1234 |
|   2   |      Mary      |   555-111-1234 |
+-------+----------------+----------------+

table : leads
+-------+-----------+---------------------+
|   id  |   name    |     phonenumber     |
+-------+-----------+---------------------+
|   1   |   John    |   214-444-1234      |
|   2   |   Mary    |   555-111-1234      |
+-------+-----------+---------------------+

table : customvaluefields
+-------+-----------+-------------+-----------+
|   id  |   relid   |   fieldid   |   value   |
+-------+-----------+-------------+-----------+
|   1   |     1     |     41      | 222333444 |
|   2   |     1     |     20      | Management|
|   3   |     2     |     41      | 333444555 |
+-------+-----------+-------------+-----------+

Current working queries当前工作查询

$result = $CI->db->query('SELECT * FROM ' . db_prefix() . 'contacts 
            WHERE replace(replace(phonenumber, " ", ""), "-", "") = ' . $phonenumber)->row();

if (count($result) == 0) {
    $result = $CI->db->query('SELECT * FROM ' . db_prefix() . 'leads 
            WHERE replace(replace(phonenumber, " ", ""), "-", "") = ' . $phonenumber)->row();
            
    if (count($result) == 0) {
        $result = $CI->db->query('SELECT * FROM ' . db_prefix() . 'leads AS l 
            INNER JOIN ' . db_prefix() . 'customfieldsvalues AS c ON l.id = c.relid
            WHERE c.fieldid = 41 AND c.value = ' . $phonenumber)->row();
    }
}

If I understand what you are trying to, maybe UNION ALL would work.如果我理解你想要做什么,也许 UNION ALL 会起作用。 This is something to get you started:这是让你开始的东西:

SELECT C.ID, C.FirstName, C.Phonenumber 
FROM Contacts C 
JOIN CustomValueField CVF 
ON c.ID = CVF.RelID AND 
    CVF.ID = 41
    AND REPLACE(Phonenumber,'-','') = cvf.Value 
UNION ALL 
    SELECT L.ID, L.FirstName, L.Phonenumber 
FROM Leads L
JOIN CustomValueField CVF 
ON L.ID = CVF.RelID AND 
    CVF.ID = 41
    AND REPLACE(Phonenumber,'-','') = cvf.Value 

I'm joining the contacts and leads tables to CustomeValueField in each query and then UNION them together along with the WHERE clause in each.我在每个查询中将联系人和潜在客户表加入到 CustomeValueField,然后将它们与每个查询中的 WHERE 子句一起合并。 I'm sure it's not 100% correct for what you need, but should get you headed to a solution.我确信它不是 100% 正确满足您的需要,但应该让您找到解决方案。 Here is more information: https://dev.mysql.com/doc/refman/8.0/en/union.html这里有更多信息: https://dev.mysql.com/doc/refman/8.0/en/union.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM