Convert result of mysql query into nested php array [closed] - php

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 have a Mysql query:
SELECT
`pcm`.`ContactMechanismId` AS `ContactMechanismId`,
`tel`.`AreaCode` AS `AreaCode`,
`tel`.`PhoneNbr` AS `PhoneNbr`,
`tel`.`Extension` AS `Extension`,
`cmt`.`ContactMechanismTypeName` AS `ContactMechanismTypeName`,
`c`.`DialingCode` AS `DialingCode`,
`rt`.`RoleTypeName` AS `RoleTypeName`,
`cmpt`.`ContactMechanismPurposeTypeName` AS `ContactMechanismPurposeTypeName`
FROM
`PartyContactMechanisms` AS `pcm`
INNER JOIN
`Telephones` AS `tel` ON `pcm`.`ContactMechanismId` = `tel`.`ContactMechanismId`
INNER JOIN
`ContactMechanisms` AS `cm` ON `cm`.`ContactMechanismId` = `pcm`.`ContactMechanismId`
INNER JOIN
`ContactMechanismTypes` AS `cmt` ON `cmt`.`ContactMechanismTypeId` = `cm`.`ContactMechanismTypeId`
INNER JOIN
`Countries` AS `c` ON `tel`.`GeoId` = `c`.`GeoId`
INNER JOIN
`PartyContactMechanismPurposes` AS `pcmp` ON `pcm`.`ContactMechanismId` = `pcmp`.`ContactMechanismId`
INNER JOIN
`RoleTypes` AS `rt` ON `rt`.`RoleTypeId` = `pcm`.`RoleTypeId`
INNER JOIN
`ContactMechanismPurposeTypes` AS `cmpt` ON `pcmp`.`PurposeTypeId` = `cmpt`.`PurposeTypeId`
The result I get from Mysql is
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+
| ContactMechanismId | AreaCode | PhoneNbr | Extension | ContactMechanismTypeName | DialingCode | RoleTypeName | ContactMechanismPurposeTypeName |
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+
| 2 | 111 | 22222222 | 2222 | landline | 375 | customer | common phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | contractor | common phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | manufacturer | common phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | customer | primary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | contractor | primary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | manufacturer | primary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | customer | secretary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | contractor | secretary phone |
| 2 | 111 | 22222222 | 2222 | landline | 375 | manufacturer | secretary phone |
| 1 | 17 | 2905950 | | landline | 375 | customer | other phone |
+--------------------+----------+----------+-----------+--------------------------+-------------+---------------+---------------------------------+
I need is to convert the above result into the following array:
[
[ContactMechanismId, AreaCode, PhoneNbr, Extension, ContactMechanismTypeName, DialingCode, RoleTypeName[], ContactMechanismPurposeTypeName[]
]
In the arrays RoleTypeName[] and ContactMechanismPurposeTypeName[] go ALL possible values for a certain ContactMechanismId from the corresponding column of the mysql result.
How can I do this?

You can change the select statement like bellow using GROUP_CONCAT()
SELECT
`pcm`.`ContactMechanismId` AS `ContactMechanismId`,
`tel`.`AreaCode` AS `AreaCode`,
`tel`.`PhoneNbr` AS `PhoneNbr`,
`tel`.`Extension` AS `Extension`,
`cmt`.`ContactMechanismTypeName` AS `ContactMechanismTypeName`,
`c`.`DialingCode` AS `DialingCode`,GROUP_CONCAT(
`rt`.`RoleTypeName`) AS `RoleTypeName`,GROUP_CONCAT(
`cmpt`.`ContactMechanismPurposeTypeName`) AS `ContactMechanismPurposeTypeName`
Then group the data using GROUP BY,You will get RoleTypeName and ContactMechanismPurposeTypeName as a comma seperated value.You can iterate using php loop and using explode() function to form the array

Related

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'

Using LEFT OUTER JOIN return one matched row and additional only matching id's

I have a large database that has about 190 columns in 22 tables. There are a few tables that allow multiple entries into the database all of the values are referenced by a foreign key. When I use a LEFT OUTER JOIN If there are multiple entries in a single column matching a specific ID then it creates a new row with all of the information as before only changing the table fields. For example:
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
| CompanyID | Name | Address | City | State | Zip | Country | Website |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
| 227 | Hello Company | 123 blvd | Boom | OK | 56008 | USA | www.imtired.com |
| 228 | Test Company | 87 Wesley Street | Denham | LA | 21726 | USA | www.tests.com |
| 229 | Testing Company | 2 US hwy 281 N. | Antonio | TX | 64258 | USA | www.modeling.com |
| 230 | TestCompany | 45 W. 95th St | Oak Lawn | IL | 61453 | USA | www.express.com |
| 235 | Encore | 2142 S. Patterson | City | IA | 43106 | USA | www.boomsite.com |
| 235 | Encore | 2142 S. Patterson | City | IA | 43106 | USA | www.testingsite.com |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
You see that the company Encore has two rows with only the website being different is there a way to make it like this:
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
| CompanyID | Name | Address | City | State | Zip | Country | Website |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
| 227 | Hello Company | 123 blvd | Boom | OK | 56008 | USA | www.imtired.com |
| 228 | Test Company | 87 Wesley Street | Denham | LA | 21726 | USA | www.tests.com |
| 229 | Testing Company | 2 US hwy 281 N. | Antonio | TX | 64258 | USA | www.modeling.com |
| 230 | TestCompany | 45 W. 95th St | Oak Lawn | IL | 61453 | USA | www.express.com |
| 235 | Encore | 2142 S. Patterson | City | IA | 43106 | USA | www.boomsite.com |
| | | | | | | | www.testingsite.com |
+-----------+---------------------------+-----------------------------+----------------+-------+-------+---------+-------------------------------+
This is a snippet of the query I am using :
SELECT * FROM `company` C
LEFT OUTER JOIN owner O USING ( CompanyID )
LEFT OUTER JOIN sales S USING ( CompanyID )
You can try something like this in mySQL. It will give comma separated websites whenever there are multiple rows with everything same except the website
SELECT *,GROUP_CONCAT(website,',') as website FROM `company` C
LEFT OUTER JOIN owner O USING ( CompanyID )
LEFT OUTER JOIN sales S USING ( CompanyID )
GROUP BY CompanyID

concat group a SELECT in another SELECT [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Simple question:
There are 2 tables:
Genre [name,songID]
Song [id,title,userID,status]
SELECT id,name,title,userID,status FROM songs INNER JOIN genre ON song.id=genre.songID ORDER BY id ASC;
What is the query to get a result from
+----+-------------+----------------------+--------+--------+
| id | genre.name | song.title | userID | status |
+----+-------------+----------------------+--------+--------+
| 1 | tech | Feel it all | 1 | 1 |
| 2 | tech | Tester | 1 | 1 |
| 3 | music | Sejujurnya | 1 | 1 |
| 4 | music | Not Done | 1 | 1 |
| 5 | life | Cinta | 1 | 1 |
| 6 | life | Feel it all | 1 | 1 |
| 7 | life | Not Done | 1 | 1 |
| 8 | truth | Tester | 1 | 1 |
| 9 | tree | Tester | 1 | 1 |
| 10 | climb | Tester | 1 | 1 |
+----+-------------+----------------------+--------+--------+
to
+----+-------------+---------------------------------+--------+--------+
| id | genre.name | song.title | userID | status |
+----+-------------+---------------------------------+--------+--------+
| 1 | tech | Feel it all,Tester | 1 | 1 |
| 2 | music | Sejujurnya, Not Done | 1 | 1 |
| 3 | life | Cinta, Feel it all, Note Done | 1 | 1 |
| 4 | truth | Tester | 1 | 1 |
| 5 | tree | Tester | 1 | 1 |
| 6 | climb | Tester | 1 | 1 |
+----+-------------+---------------------------------+--------+--------+
Thanks
Use GROUP_CONCAT with GROUP BY
SELECT
id,
genre.name,
GROUP_CONCAT(title) as title,
userID,
status
FROM
songs
INNER JOIN
genre
ON
song.id=genre.songID
GROUP BY
genre.name
ORDER BY
id ASC
SELECT
`Songs`.`id`,
`Genre`.`Name`,
GROUP_CONCAT(`Songs`.`Title`) as Title,
`Songs`.`userID`,
`Songs`.`status`
FROM
`Genre`,
`Songs`
WHERE
`Genre`.`SongID` = `Songs`.`id`
GROUP BY
`Genre`.`Name`

MYSQL: Search for User ID in one table to search for data in 2 other tables, then show data from all 3 tables

I have a database with entries similar to the following...
users:
+-----+------+----------+---------+-------+
| UID | Name | Addr | City | State |
+-----+------+----------+---------+-------+
| 1 | John | 101 Main | Austin | TX |
| 2 | John | 101 Main | Houston | TX |
| 3 | John | 101 Main | Del Rio | TX |
| 4 | John | 101 Main | Houston | TX |
+-----+------+----------+---------+-------+
verification:
+-----+---------------+--------------+
| UID | LicenseFirst3 | LicenseLast3 |
+-----+---------------+--------------+
| 1 | 554 | 122 |
| 2 | 556 | 345 |
| 3 | 555 | 382 |
| 4 | 555 | 108 |
+-----+---------------+--------------+
section_user_map:
+-----+-----------+---------------------+
| UID | SectionID | CompleteDate |
+-----+-----------+---------------------+
| 1 | 65 | 2012-05-12 05:05:15 |
| 2 | 72 | 2012-05-06 14:03:15 |
| 3 | 65 | 2012-05-09 16:13:15 |
| 4 | 72 | 2012-05-06 18:14:15 |
+-----+-----------+---------------------+
I need to be able to search for students who completed section 65 between noon on day X and noon on day Y. I also need to show the student's name, address, city, state and first and last three digits of their license number. I believe this will require both a left join and union command but it's getting a bit too complicated to formulate.
SELECT *
FROM section_user_map
JOIN users USING (UID)
JOIN verification USING (UID)
WHERE SectionID = 65
AND CompleteDate BETWEEN '2012-05-09 12:00:00'
AND '2012-05-11 12:00:00'
See it on sqlfiddle.
No UNION required. Outer join would only be required if you still want to return results for users who do not exist in one (or both) of the users or verification tables.

mysql inner join 2 tables and order by count

I am having the following tables in my DB
PROJECTS
+----+-------------------------------------------+
| id | name |
+----+-------------------------------------------+
| 1 | YANNONALI COURT |
| 2 | UNIVERSITY OF COLORARDO DENVER RESEARCH 2 |
| 3 | G.R.E.A.T PROGRAM DESALTER BUILDING |
| 4 | MONARCH CLUB |
| 5 | LAFAYETTE MERCANTILE |
| 6 | CAMELBACK VILLAGE RAQUET AND HEALTH CLUB |
| 7 | BACK COUNTRY |
| 8 | URBAN CRASHPAD |
| 9 | PRIVATE RESIDENCE |
| 10 | EATON RESIDENCE |
+----+-------------------------------------------+
PROJECT_ASSIGNMENTS(WHERE projects.id=project_assignment.target_id)
+-------+-----------+-------------+
| id | target_id | property_id |
+-------+-----------+-------------+
| 19178 | 1 | 48 |
| 19192 | 1 | 39 |
| 19391 | 1 | 3 |
| 19412 | 2 | 3 |
| 19591 | 2 | 34 |
| 19610 | 2 | 34 |
| 21013 | 3 | 2 |
| 21032 | 3 | 2 |
| 30876 | 4 | 2433 |
| 38424 | 5 | 2580 |
+-------+-----------+-------------+
PROPERTIES(WHERE properties.id= project_assignment.property_id)
+----+------------------+
| id | name |
+----+------------------+
| 2 | Residential |
| 3 | Multi Family |
| 34 | New Construction |
| 39 | Contemporary |
| 48 | Southwest |
+----+------------------+
I want O/P ordered by no.of projects in the list...
Residential(177) //12 - total no.of projects which is having this property
Multi Family(15)
New Construction(13)
Contemporary(11)
please give me some MySQL queries
Thank You
This should do the trick:
select
c.name,
count(c.id) as CountOfProperties
from
projects a,
project_assignments b,
properties c
where
a.ID=b.target_id
and b.property_id=c.ID
group by
c.name
order by
count(c.id) desc;
Try this::
select
prop.name,
count(prop.id) as CountOfProperties
from
projects p
inner join project_assignments pa on (p.ID=pa.target_id)
inner join properties prop on (pa.property_id=prop.ID)
group by
prop.name
order by
count(prop.id) desc;

Categories