Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:58:27+00:00 2026-06-09T20:58:27+00:00

I have two Tables: Table-1: notes Table-2: contacts My basic objective is to perform

  • 0

I have two Tables:

Table-1: notes
enter image description here

Table-2: contacts
enter image description here

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:
4000
4001

Also I dont want to repeat contact_id in Output.
Please suggest.

Code After correction by 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). For example it showed following output when i gave search parameter nawaz:
4001
4001
4001
4001
4001
4002
4003

Thanks for your help, please help me out to fix this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:58:29+00:00Added an answer on June 9, 2026 at 8:58 pm

    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.

    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. (Azeem = 4000, but no note_contact exists for user 4000).

    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.

    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")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
My Schema I have the following tables table notes/example values ------------------------------------------------ users ( id
I have two tables containing Tasks and Notes, and want to retrieve a list
I have two tables from which I'm trying to run a query to return
So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);
I have two tables people name id man1 456 man2 123 man3 789 notes
I have two tables people name id man1 456 man2 123 man3 789 notes
Let's say I have two tables, users and notes. Let's say the schemas look
I have two tables: table a ida valuea 1 a 2 b 3 c
I have two tables: Table GL TRANS_NBR BASE_AMOUNT CTRL GRP 120211282 -7200 77 120211282

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.