简体   繁体   English

加入两个表问题

[英]Joining two tables Issue

I have two Tables: 我有两张桌子:

Table-1: notes 表-1:注释
在此输入图像描述

Table-2: contacts 表-2:联系人
在此输入图像描述


My basic objective is to perform following query: 我的基本目标是执行以下查询:
list * from contacts where (search text exists in contacts) or (search text exists in notes for that contact) 列表*来自联系人(搜索文本存在于联系人中)或(搜索文本存在于该联系人的备注中)

I have written following code to do this: 我写了以下代码来执行此操作:

<?php
require_once('config.php');

$nwhere = "WHERE note_text LIKE '%".addslashes($_GET['s'])."%' ";
$cwhere = "contact_text LIKE '%".addslashes($_GET['s'])."%' ";
$result = mysql_query("SELECT * FROM contacts INNER JOIN notes ON contact_id = note_contact $nwhere OR $cwhere ORDER BY contact_id");

while($row = mysql_fetch_array($result))
{
echo $row['contact_id'];
echo '<br/>';
}
?>

When Search Text is azeem , the above code prints only 4001 , however output should be: 当搜索文本是azeem ,上面的代码只打印4001 ,但输出应该是:
4000 4000
4001 4001

Also I dont want to repeat contact_id in Output. 另外,我不想在输出中重复contact_id
Please suggest. 请建议。



Code After correction by Fluffeh : 代码经过Fluffeh校正后:

$where_clause = " where contacts.contact_text like '%".addslashes($_GET['s'])."%' or    notes.note_text like '%".addslashes($_GET['s'])."%'";
$result = mysql_query("select notes.note_id, notes.note_contact, contacts.contact_id,  contacts.contact_text, notes.note_text from contacts left outer join notes on contacts.contact_id=notes.note_contact $where_clause");
while($row = mysql_fetch_array($result))
{
echo $row['contact_id'];
echo '<br/>';
}
?>


This code picks up correct rows from tables but there is one minor issue that it repeats the output (contact_id). 此代码从表中获取正确的行,但有一个小问题,它重复输出(contact_id)。 For example it showed following output when i gave search parameter nawaz : 例如,当我给出搜索参数nawaz时它显示以下输出:
4001 4001
4001 4001
4001 4001
4001 4001
4001 4001
4002 4002
4003 4003

Thanks for your help, please help me out to fix this. 感谢您的帮助,请帮我解决这个问题。

If you don't want a column repeated, you can't use SELECT * FROM but rather you will need to use the column names you want to select. 如果您不希望重复列,则不能使用SELECT * FROM ,而是需要使用要选择的列名。

You aren't getting the 4000 result you are expecting because you are doing an inner join on a field that doesn't exist in the other table. 您没有得到您期望的4000结果,因为您正在另一个表中不存在的字段上进行内部联接。 (Azeem = 4000, but no note_contact exists for user 4000). (Azeem = 4000,但用户4000不存在note_contact)。

You should consider switching to an outer join instead. 您应该考虑切换到外部联接。 Maybe something like this: 也许是这样的:

select
    a.note_id,
    a.note_contact,
    b.contact_text,
    b.note_text
from
    contacts a
        left outer join notes b
            on a.contact_id=b.note_contact
where
    a.contact_text like '%azeem%'
    or b.note_text like '%azeem%'

Edit: Seems we were both still working - I gave made a sqlFiddle which has the basic schema and a working outer join for the example. 编辑:似乎我们都还在工作 - 我给了一个sqlFiddle ,它有一个基本的模式和一个工作的外连接作为例子。

My create schema is: 我的创建架构是:

mysql> CREATE TABLE `contacts` (
    ->   `contact_id` int(4) DEFAULT NULL,
    ->   `contact_text` varchar(40) DEFAULT NULL,
    ->   `contact_email` varchar(40) DEFAULT NULL
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> CREATE TABLE `notes` (
    ->   `note_id` int(3) NOT NULL AUTO_INCREMENT,
    ->   `note_contact` int(4) DEFAULT NULL,
    ->   `note_text` tinytext,
    ->   PRIMARY KEY (`note_id`)
    -> ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> INSERT INTO `contacts` (`contact_id`, `contact_text`, `contact_email`) VALUES
    -> (4000, 'azeem', 'azeem@big.com'),
    -> (4001, 'nawaz', 'azeem@big.com'),
    -> (4002, 'nawaz', 'azeem@big.com');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> 
mysql> INSERT INTO `notes` (`note_id`, `note_contact`, `note_text`) VALUES
    -> (1, 4001, 'I am text1'),
    -> (2, 4001, 'I am text2'),
    -> (3, 4001, 'my name is azeem'),
    -> (4, 4001, 'come here'),
    -> (5, 4001, 'I don''t want to'),
    -> (6, 4003, 'My text is clear.');
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

outer Join Query: 外连接查询:

mysql> select
    -> b.note_id,
    -> b.note_contact,
    -> a.contact_text,
    -> b.note_text
    -> from
    -> contacts a
    -> left outer join notes b
    -> on a.contact_id=b.note_contact
    -> where
    -> a.contact_text like '%azeem%'
    -> or b.note_text like '%azeem%';
+---------+--------------+--------------+------------------+
| note_id | note_contact | contact_text | note_text        |
+---------+--------------+--------------+------------------+
|    NULL |         NULL | azeem        | NULL             |
|       3 |         4001 | nawaz        | my name is azeem |
+---------+--------------+--------------+------------------+
2 rows in set (0.00 sec)

Code working from Nida: 代码在奈达工作:

$where_clause = " where contacts.contact_text like '%".addslashes($_GET['s'])."%' or notes.note_text like '%".addslashes($_GET['s'])."%'";
$result = mysql_query("select distinct contacts.contact_id from contacts left outer join notes on contacts.contact_id=notes.note_contact $where_clause")

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

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