Combining results on a MySQL INNER JOIN into one PHP array
I have two tables that I would like to join into one, but not all the fields. I've used INNER JOIN with some success, but can't get the exact results I need. Essentially, when using PHP to return results, I would like the key to be the 'meta_key' value.
Below are the two tables I want to combine:
USERS
+--------+-----------------+------------------+----------------+
| ID | username | first_name | last_name |
+--------+-----------------+------------------+----------------+
| 2 | hthompson | Hunter | Thompson |
| 7 | coak | Carol | Oak |
| 8 | delk | Dannie | Elk |
| 9 | mride | Mark | Ride |
| 10 | kken | Kyle | Ken |
| 11 | glee | Ginny | Lee |
| 12 | nwatts | Naomi | Watts |
| 13 | jwong | Jin | Wong |
| 14 | syin | Shen | Yin |
+--------+-----------------+------------------+----------------+
USERS_META
+--------+--------+-----------------+------------------+
| ID | UID | meta_key | meta_value |
+--------+--------+-----------------+------------------+
| 1 | 2 | business_name | Company Inc. |
| 2 | 2 | city | New York |
| 3 | 2 | state | NY |
| 5 | 9 | city | Boston |
| 6 | 9 | state | MA |
| 7 | 11 | business_type | Printer |
| 8 | 8 | chamber_member | true |
| 9 | 2 | business_type | Design |
+--------+--------+-----------------+------------------+
Below is an example of what I'd like to return:
USERS
+--------+-----------------+------------+------------+------------------+
| ID | username | city | state | business_name |
+--------+-----------------+------------+------------+------------------+
| 2 | hthompson | New York | NY | Company Inc. |
+--------+-----------------+------------+------------+------------------+
OR
$user['ID'] = 2
$user['username'] = hthompson
$user['city'] = New York
$user['state'] = NY
$user['business_name'] = Company Inc.
The closest I've come is this:
$query = ("SELECT *
FROM users
INNER JOIN users_meta ON users.ID = users_meta.UID
WHERE
users_meta.meta_key = 'city' OR
users_meta.meta_key = 'state' OR
users_meta.meta_key = 'business_name'
");
However, doing such returns three results for each unique user ID, and I'm aiming to returning one with all the meta info 开发者_开发问答specified. The primary purpose of this is so that I will be able to search using a keyword, which would apply to the USERS.first_name, USERS.first_name and USERS_META.business_name columns and then obviously return results in a table showing ID, Business Name, City, State, First & Last Name.
Thanks in advance!
You can try this:
SELECT
u.ID,
u.username,
m0.meta_value as city,
m1.meta_value as state,
m2.meta_value as business_name
FROM
users u,
users_meta m0,
users_meta m1,
users_meta m2
WHERE
u.ID = m0.UID
AND m0.meta_key = 'city'
AND u.ID = m1.UID
AND m1.meta_key = 'state'
AND u.ID = m2.UID
AND m2.meta_key = 'business'
As far as getting the information out I would just suggest joining the table together and using php to make the array. Something similar to this:
<?php
//if search is blank, returns all rows
$search = isset($_POST['search'])?mysql_real_escape_string($_POST['search']):'';
//query for all rows with meta key/values including extra
//rows from the join. extra rows will be taken out later.
$result = mysql_query(
"SELECT
U.ID,
U.USERNAME,
U.FIRST_NAME,
U.LAST_NAME,
UM.META_KEY,
UM.META_VALUE
FROM USERS U
JOIN USERS_META UM ON U.ID=UM.UID
WHERE
UM.META_KEY IN ('city', 'state', 'business_name')
AND U.FIRST_NAME LIKE '%{$search}%'");
//an empty array to put our results into
$out = array();
//loop through the rows
while($row=mysql_fetch_assoc($out)){
//check if the user id has been added already
if(!isset($out[$row['ID']])){
//if not, add it with generic information
$out[$row['ID']] = array(
'ID'=>$row['ID'],
'USERNAME'=>$row['USERNAME'],
'FIRST_NAME'=>$row['FIRST_NAME'],
'LAST_NAME'=>$row['LAST_NAME']);
}
//add the meta key and value
$out[$row['ID']][$row['META_KEY']] = $row['META_VALUE'];
}
//display
echo '<pre>'.print_r($out,1).'</pre>';
?>
精彩评论