如何知道该行属于哪个表?

时间:2014-05-20 09:58:43

标签: php mysql sql search

我正在尝试对3个不同的表进行搜索查询。 它正在工作,但现在我无法弄清楚如何知道该行所属的表。任何想法?

这是我的问题:

$query = $db->query("SELECT p_name,l_name,phone1,phone2,phone3,city,building,project,dira FROM `clients_to_call` t1 WHERE 
            (t1.p_name LIKE '%".$_POST['p_name']."%' && t1.l_name LIKE '%".$_POST['l_name']."%' && (t1.phone1='".$_POST['phone']."' || t1.phone2='".$_POST['phone']."' || t1.phone3='".$_POST['phone']."'))
            UNION ALL
            SELECT p_name,l_name,phone1,phone2,phone3,meeting,status,city,project FROM `clients_closed_1` t2 WHERE 
            (t2.p_name LIKE '%".$_POST['p_name']."%' && t2.l_name LIKE '%".$_POST['l_name']."%' && (t2.phone1='".$_POST['phone']."' || t2.phone2='".$_POST['phone']."' || t2.phone3='".$_POST['phone']."'))
            UNION ALL
            SELECT p_name,l_name,phone1,phone2,phone3,client_number1,status,city,project FROM `clients_closed_2` t3 WHERE 
            (t3.p_name LIKE '%".$_POST['p_name']."%' && t3.l_name LIKE '%".$_POST['l_name']."%' && (t3.phone1='".$_POST['phone']."' || t3.phone2='".$_POST['phone']."' || t3.phone3='".$_POST['phone']."'))");

谢谢。

3 个答案:

答案 0 :(得分:2)

您可以为每个查询添加一个常量字符串,例如

$query = $db->query("SELECT `clients_to_call` p_name, l_name, ...

答案 1 :(得分:1)

您应该添加一个字符串,指示union的每个部分的所有者表。试试这个:

$query = $db->query("SELECT 'clients_to_call' owner_table, p_name,l_name,phone1,phone2,phone3,city,building,project,dira FROM `clients_to_call` t1 WHERE 
            (t1.p_name LIKE '%".$_POST['p_name']."%' && t1.l_name LIKE '%".$_POST['l_name']."%' && (t1.phone1='".$_POST['phone']."' || t1.phone2='".$_POST['phone']."' || t1.phone3='".$_POST['phone']."'))
            UNION ALL
            SELECT 'clients_closed_1' owner_table, p_name,l_name,phone1,phone2,phone3,meeting,status,city,project FROM `clients_closed_1` t2 WHERE 
            (t2.p_name LIKE '%".$_POST['p_name']."%' && t2.l_name LIKE '%".$_POST['l_name']."%' && (t2.phone1='".$_POST['phone']."' || t2.phone2='".$_POST['phone']."' || t2.phone3='".$_POST['phone']."'))
            UNION ALL
            SELECT 'clients_closed_2' owner_table, p_name,l_name,phone1,phone2,phone3,client_number1,status,city,project FROM `clients_closed_2` t3 WHERE 
            (t3.p_name LIKE '%".$_POST['p_name']."%' && t3.l_name LIKE '%".$_POST['l_name']."%' && (t3.phone1='".$_POST['phone']."' || t3.phone2='".$_POST['phone']."' || t3.phone3='".$_POST['phone']."'))");

答案 2 :(得分:0)

您可以对每个查询使用常量字符串:

SELECT *,'clients_to_call' FROM `clients_to_call` WHERE 1