SQL sentence for two non relational tables for seacher - php

I'm doing a searcher for my webpage and i'm facing a little issue, i want to get the info from two non relationals tables but the data is not returning the way i want
table 1
ID |FNAME |LNAME | STATE | CITY
------------------------------
1 |xxxxx1 |xxxxx1| xxxx1 | xx1
2 |xxxxx2 |xxxxx2| toronto| xx2
3 |xxxxx3 |xxxxx3| xxxx3 | yy3
4 |zzzzz3 |zzzzz3| toronto| yy3
table 2
ID |NAME | STATE | CITY
---------------------
1 |yyyyy1 | yyyy1 | yy1
2 |yyyyy2 | yyyy2 | yy2
3 |yyyyy3 | toronto| yy3
currently i have
SELECT
e.id_client, e.fname_client, e.city_client, e.state_client , m.id_client, m.fname_client, m.lname_client, m.state_client, m.city_client
FROM
empresas e
CROSS JOIN
medicos m
WHERE
e.fname_client LIKE :busqueda
OR
e.city_client LIKE :busqueda
OR
m.fname_client LIKE :busqueda
OR
m.lname_client LIKE :busqueda
OR
m.state_client LIKE :busqueda
OR
m.city_client LIKE :busqueda
this displays the result like this
ID| FNAME |LNAME | STATE | CITY |ID|FNAME |LNAME | STATE | CITY
3 |yyyyy3 | | yyyy3 | yy3 |3 |xxxxx3 |xxxxx3| xxxx3 | yy3
but i want it like this
ID|FNAME |LNAME | STATE | CITY
3 |xxxxx3 |xxxxx3| xxxx3 | yy3
3 |yyyyy3 | | yyyy3 | yy3
EDIT:
with the UNION asnwer i get the data with the format i want, but is showing only the results from one table when i search for commons values, for example:
if i type "Toronto", this must shows
ID|FNAME |LNAME | STATE | CITY
2 |xxxxx3 |xxxxx3| toronto | yy3 <- doctor
3 |yyyyy3 | | toronto | yy3 <- organization
4 |zzzzz3 |zzzzz3| toronto | yy3
but is only showing this
ID|FNAME |LNAME | STATE | CITY
2 |xxxxx3 |xxxxx3| toronto | yy3 <- doctor
4 |zzzzz3 |zzzzz3| toronto | yy3 <- doctor

I guess you want to concatenate the two tables. Then...
select
id, fname, lname, state, city
from
table1
where
<your where condition here>
union all
select
id, fname, lname, state, city
from
table2
where
<your other where condition here>
EDIT
If you have:
SQL> select * from table1 order by id;
id | fname | lname | state | city
----+------------+------------+------------+------------
1 | xxxxx1 | xxxxx1 | xxxx1 | xx1
2 | xxxxx2 | xxxxx2 | toronto | xx2
3 | xxxxx3 | xxxxx3 | xxxx3 | xx3
4 | zzzzz3 | zzzzz3 | toronto | yy3
SQL> select * from table2 order by id;
id | name | state | city
----+------------+------------+------------
1 | yyyyy1 | yyyy1 | yy1
2 | yyyyy2 | yyyy2 | yy2
3 | yyyyy3 | toronto | yy3
then:
select
id, fname, lname, state, city
from
table1
where
state='toronto'
union all
select
id, name as fname, NULL as lname, state, city
from
table2
where
state='toronto'
order by id;
id | fname | lname | state | city
----+------------+------------+------------+------------
2 | xxxxx2 | xxxxx2 | toronto | xx2
3 | yyyyy3 | (null) | toronto | yy3
4 | zzzzz3 | zzzzz3 | toronto | yy3

Related

php: how to fetch multiple tables in mysqli

we have two table for example persons1 and persons2 and out put should be rows of those tables
for exemple:
`persons1`
+----+-------------+
| id | name |
+----+-------------+
| 1 | john |
| 2 | Sophia |
+----+-------------+
and
`persons2`
+----+-------------+
| id | the_name |
+----+-------------+
| 1 | Olivia |
| 2 | Jackson |
+----+-------------+
Output is :
array(
[1,'john'],
[2,'Sophia'],
[1,'Olivia'],
[2,'Jackson'],
)
thank you
You can use union like this:
SELECT id, name as `the_name` FROM `persons1`
UNION
SELECT id, the_name FROM `persons2`;
which outputs:
| id | the_name |
|----| ---------|
| 1 | John |
| 2 | Sophia |
| 1 | Olivia |
| 2 | Jackson |
SqlFiddle
SELECT p1.id, p1.name AS `the_name` FROM persons1 p1
UNION
SELECT p2.id, p2.the_name FROM persons2 p2;

How can I divide table data into multiple tables and keep the relations?

I have four tables: (three o them are empty)
-- all_locations
+----+----------------+---------------+-----------+
| id | name | location_type | parent_id |
+----+----------------+---------------+-----------+
| 1 | Aruba | 0 | 0 |
| 2 | Afghanistan | 0 | 0 |
| 3 | – | 1 | 1 |
| 4 | Balkh | 1 | 2 |
| 5 | Herat | 1 | 2 |
| 6 | Kabol | 1 | 2 |
| 7 | South Hill | 2 | 3 |
| 8 | The Valley | 2 | 3 |
| 9 | Mazar-e-Sharif | 2 | 4 |
| 10 | Herat | 2 | 5 |
+----+----------------+---------------+-----------+
-- country
+----+---------+
| id | name |
+----+---------+
-- province
+----+---------+-------------+
| id | name | country_id |
+----+---------+-------------+
-- city
+----+---------+-------------+
| id | name | province_id |
+----+---------+-------------+
Now I want to divide data of the first table between other tables (based on the location_type valye: 0 means country, 1 means province, 2 means city)
and then drop the first table.
So this is the expected result:
-- country
+----+-------------+
| id | name |
+----+-------------+
| 1 | Aruba |
| 2 | Afghanistan |
+----+-------------+
-- province
+----+---------+------------+
| id | name | country_id |
+----+---------+------------+
| 1 | – | 1 |
| 2 | Balkh | 2 |
| 3 | Herat | 2 |
| 6 | Kabol | 2 |
+----+---------+------------+
-- city
+----+----------------+-------------+
| id | name | province_id |
+----+----------------+-------------+
| 1 | South Hill | 1 |
| 2 | The Valley | 1 |
| 3 | Mazar-e-Sharif | 2 |
| 4 | Herat | 3 |
+----+----------------+-------------+
I can do that by a loop in PHP. Something like this:
foreach( $fetched_rows_from_all_locations as $location ){
switch( $location['location_type'] ){
case '0':
-- INSERT INTO country (name) VALUES ($location['name'])
break;
case '1':
-- INSERT INTO province(name) VALUES ($location['name'])
break;
case '2':
-- INSERT INTO city(name) VALUES ($location['name'])
break;
}
}
But my code doesn't handle the relations. As you know, the values into parent_id column aren't valid anymore. So I have to make the new ids. How can I do that?
My solution would be:
Inserting the countries to the new table - which is the easiest task.
Then inserting the province. Now we have to grab the new IDs of the countries (as the ID column is auto-increment).
First we do a self-JOIN on all_location, to get the parents name.
Then we do a join to the previous new filled table countries on the column name, grab the ID and finally insert the values to the new province table.
Same procedure like above again for the city table.
INSERT INTO country (name)
SELECT name
FROM all_locations
WHERE location_type = 0
INSERT INTO province(name, country_id)
SELECT child.name
,country.id
FROM all_locations child
INNER JOIN all_locations parent
ON child.parent_id = parent.id
INNER JOIN country
ON country.name = parent.name
WHERE child.location_type = 1
INSERT INTO city(name, province_id)
SELECT child.name
,province.id
FROM all_locations child
INNER JOIN all_locations parent
ON child.parent_id = parent.id
INNER JOIN province
ON province.name = parent.name
WHERE child.location_type = 2
DROP TABLE all_locations
If you want to reproduce my solution, you can use this link:
http://rextester.com/XQOZ12449

SQL search user and user state from city_id [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I don't know if the title makes any sense, but here is the situation: See 4 tables below
User Table(There is 200 + user)
+-------+---------+-------------------+---------+
|iduser | user | address | idcity |
+-------+---------+-------------------+---------+
| 1 | person1 | some address | 123 |
| 2 | person2 | another address | 542 |
| 3 | person3 | different address | 623 |
+-------+---------+-------------------+---------+
City Table
+---------+-------+----------+
| idcity | city | idstate |
+---------+-------+----------+
| 123 | city1 | 1 |
| 542 | city2 | 2 |
| 623 | city3 | 3 |
+---------+-------+----------+
State Table
+---------+--------+------------+
| idstate | state | idcountry |
+---------+--------+------------+
| 1 | state1 | 4 |
| 2 | state2 | 5 |
| 3 | state3 | 6 |
+---------+--------+------------+
Country Table
+-----------+----------+
| idcountry | country |
+-----------+----------+
| 4 | country1 |
| 5 | country2 |
| 6 | country3 |
+-----------+----------+
NOTE = I need to find out all people that belong to State and search by idcity
Output
+--------+----------+-------------------+---------+---------+
| iduser | user | address | idcity | state |
+--------+----------+-------------------+---------+---------+
| 11 | person11 | some address | 123 | state1 |
| 2 | person2 | another address | 123 | state1 |
| 13 | person13 | different address | 123 | state1 |
+--------+----------+-------------------+---------+---------+
or
+--------+----------+-------------------+---------+---------+
| iduser | user | address | idcity | state |
+--------+----------+-------------------+---------+---------+
| 20 | person20 | some address | 542 | state3 |
| 12 | person12 | another address | 542 | state3 |
| 33 | person33 | different address | 542 | state3 |
+--------+----------+-------------------+---------+---------+
How to achieve this with a single JOIN?I tried combining the two table within a JOIN(in query) which doesn't feel right somehow. Please Help how can i fix this pblm. Thankyou
Try like this,
select u.iduser,u.user,u.address,u.idcity,s.state
From usertable u
inner join city c on c.idcity=u.idcity
inner join state s on s.idstate=c.idstate
To get the city id
select
c.idcity
from city c
where c.idcity = <someidcity>
To join state name
select
c.idcity,
s.state
from city c
join state s on s.idstate = c.idstate
where c.idcity = <someidcity>
Then to join user
select
u.iduser,
u.user,
u.address,
c.idcity,
s.state
from city c
join state s on s.idstate = c.idstate
join user u on u.idcity = c.idcity
where c.idcity = <someidcity>
You can't do that in a single join, as you wanna infos (other than ids) from table user and table state, which have no direct relationship.
Try this
SELECT
a.iduser,
a.user,
a.address,
b.idcity,
c.state
FROM
users a
INNER JOIN
cities b
ON
a.idcity=b.idcity
INNER JOIN
states c
ON
b.idstate=c.idstate
WHERE
c.state='myState'
AND b.city='myCity'

MySQL query to get data from 5 tables?

What is the right MySQL query for this? Here's what I want to achieve.
I would like to display all status update entries, date posted, the
poster username, gender, city, and tags
I would like to display all status update entries by a member with user_id='1', the date the status update was posted, the poster username, gender, city, and tags that member has on his profile
And how can I display all the tags that the user have on their profile, in one line separated by ','? I tried group_concat earlier but i couldn't make it work. please help.
Here's my tables
STATUS_UPDATE Table
+------------------------------------------------------+
| status_id | user_id | body | postdate |
+-----------+---------+-----------------+--------------+
| 1 | 1 | hello world | Aug 12, 2012 |
+-----------+---------+-----------------+--------------+
| 2 | 1 | i miss you | Aug 13, 2012 |
+-----------+---------+-----------------+--------------+
| 3 | 2 | lorem ipsum | Aug 14, 2012 |
+-----------+---------+-----------------+--------------+
| 4 | 2 | why me? why? | Aug 14, 2012 |
+-----------+---------+-----------------+--------------+
MEMBERS Table //Primary data of members
+---------------------------------------------------+
| user_id | username | password | email |
+---------+----------+------------+-----------------+
| 1 | john_doe | qwerty | john#doe.com |
+---------+----------+------------+-----------------+
| 2 | maryjane | pass123 | mary#jane.com |
+---------+----------+------------+-----------------+
MEMBERS_DATA Table //Profile Fields
+----------------------------------------------+
| user_id | gender | description | city |
+---------+--------+---------------+-----------+
| 1 | male | i am simple | chicago |
+---------+--------+---------------+-----------+
| 2 | female | i am flirty | newyork |
+---------+--------+---------------+-----------+
MEMBERS_TAGS Table //tags that members added to their profiles
+-------------------+
| user_id | item_id |
+---------+---------+
| 1 | 555 |
+---------+---------+
| 1 | 666 |
+---------+---------+
| 1 | 7777 |
+---------+---------+
| 2 | 8888 |
+---------+---------+
TAGS Table //the info of the tags
+------------------------------+
| item_id | cat_id | name |
+---------+--------+-----------+
| 555 | 5 | sexy |
+---------+--------+-----------+
| 666 | 5 | beauty |
+---------+--------+-----------+
| 7777 | 6 | music |
+---------+--------+-----------+
| 8888 | 6 | movies |
+---------+--------+-----------+
CATEGORY Table //the category of the tags
+-------------------------+
| cat_id | category_name |
+---------+---------------+
| 5 | appearance |
+---------+---------------+
| 6 | hobbies |
+---------+---------------+
BTW I'm coding in PHP, I want to thank you in advance for the help.
SELECT
STATUS_UPDATE.* ,
MEMBERS.username ,
MEMBERS_DATA.gender ,
MEMBERS_DATA.city
TAGS.*
FROM UPDATE
LEFT JOIN MEMBERS ON MEMBERS.user_id = STATUS_UPDATE.user_id
LEFT JOIN MEMBERS_DATA ON MEMBERS_DATA.user_id = STATUS_UPDATE.user_id
LEFT JOIN MEMBERS_TAGS ON MEMBERS_TAGS.user_id = STATUS_UPDATE.user_id
LEFT JOIN TAGS ON TAGS.item_id = MEMBERS_TAGS.item_id

mysql union query

The table that contains information about members has a structure like:
id | fname | pic | status
--------------------------------------------------
1 | john | a.jpg | 1
2 | mike | b.jpg | 1
3 | any | c.jpg | 1
4 | jacky | d.jpg | 1
Table for list of friends looks like:
myid | date | user
-------------------------------
1 | 01-01-2011 | 4
2 | 04-01-2011 | 3
I want to make a query that will as result print users from "friendlist" table that contains photos and names of that users from "members" table of both, myid (those who adding) and user (those who are added).
That table in this example will look like:
myid | myidname | myidpic | user | username | userpic | status
-----------------------------------------------------------------------------------
1 | john | a.jpg | 4 | jacky | d.jpg | 1
2 | mike | b.jpg | 3 | any | c.jpg | 1
This should do it:
SELECT
m1.id AS myid,
m1.fname AS myidname,
m1.pic AS myidpic,
m2.id AS user,
m2.fname AS username,
m2.pic AS userpic,
m1.status
FROM
members m1
INNER JOIN friends f ON m1.id = f.myid
INNER JOIN members m2 ON f.user = m2.id

Categories