vendredi 10 juin 2016

How to get result from two different table with one single input


I have two different tables with different column name

Table 1 users

+-----+----------+---------+---------+------------+
|  id |   name   | username| image   |   email    |
+-----+----------+---------+---------+------------+
|  1  |  John1   | exmple1 |  img1   |a@gmail.com |
|  2  |  John2   | exmple2 |  img2   |b@gmail.com |
|  3  |  John3   | exmple3 |  img3   |c@gmail.com |
|  4  |  John4   | exmple4 |  img4   |d@gmail.com |
+-----+----------+---------+---------+------------+

Table 2nd Company

+-----+----------+------------+---------=------+-----------+
|  id |company_name | username|  description   |   founded |
+-----+-------------+---------+----------------+-----------|
|  1  |    john1    | exmple1 |description1    |    2016   |
|  2  |CompanyName2 | exmple2 |description2    |    2016   |
|  3  |CompanyName3 | exmple3 |description3    |    2016   |
|  4  |CompanyName4 | exmple4 |description4    |    2016   |
+-----+-------------+---------+---------=------+-----------+

Now whenever a user type any input in search bar an ajax request is made to php file which checks for if that name of user exist in database or not

so for example if user type input john then a query should run which will check john in users table and company table & if there is a user name john & if there is a company named john it should fetch both the result.

how can i achieve this, i tried using UNION in my query but it said columns are different

Currently this is my query

$go = mysqli_query($connecDB, "SELECT name, img,username FROM users WHERE name LIKE '$q%' LIMIT 0,10");

Now people might be thinking what i really want! i want a single query that will check for input in both table & fetch their details


Aucun commentaire:

Enregistrer un commentaire