将MySQL INNER JOIN上的结果合并到一个PHP数组中

时间:2010-11-04 14:34:48

标签: php mysql arrays join

我有两个表,我想加入一个,但不是所有的字段。我已经使用INNER JOIN取得了一些成功,但无法获得我需要的确切结果。基本上,当使用PHP返回结果时,我希望键是'meta_key'值。

以下是我想要合并的两个表:

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           |
+--------+--------+-----------------+------------------+

以下是我想要返回的示例:

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.

我最接近的是:

$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'
 ");

但是,这样做会为每个唯一的用户ID返回三个结果,并且我的目标是返回一个包含指定的所有元信息。这样做的主要目的是,我将能够使用一个关键字进行搜索,该关键字将应用于USERS.first_name,USERS.first_name和USERS_META.business_name列,然后显然会在显示ID,商家名称,城市的表格中返回结果,州,第一&姓氏。

提前致谢!

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

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'

答案 1 :(得分:0)

至于获取信息,我建议一起加入表并使用php制作数组。类似的东西:

<?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>';
?>