一个很难的一对多mysql查询

时间:2012-06-20 20:50:05

标签: php mysql

我有两个表,一个是“用户”,另一个是“社交网络”。我想以某种格式从两者中获取数据,社交网络表将user_id作为列。

我希望最终得到像

这样的东西
array{
    0 => array{
        name => "Bob",
        id => 1,
        facebook => array{ <all data from the fb row> },
        twitter => array{ <all data from the tw row> },
        linkedin => array{ <all data from the li row> }
    },
    1 => array{
        name => "Jill",
        id => 2,
        facebook => array{ <all data from the fb row> },
        twitter => array{ <all data from the tw row> },
        linkedin => array{ <all data from the li row> }
    }
}

这可能吗?

为清晰起见编辑:

social_networks表格如下所示:

user_id,social_network,account_url等

用户表格如下:

id,name,picture等。

来自social_networks表的user_id与users表中的id匹配。每个用户在social_networks表中都可以有多行。

我想要抓取的是来自users表的整行和来自社交网络表的每一行。我更愿意在一个查询中执行此操作。

4 个答案:

答案 0 :(得分:2)

你不能直接通过mysql来做,但你可以很容易地做到这一点。这是伪代码:

// connect to database
// query for all users
// foreach user
  // add user to array
  // add user's fb info to array
  // add user's tw info to array
  // add user's li info to array

稍微详细一点(使用PDO,因为我甚至不会假装mysql_ *没问题)。

<?php

// create a PDO object to connect to the db
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');

// get a basic list of users with their IDs and names
$query = $pdo->query('select user_id, name from users');

// prepare the final array that will hold it all
$users = array();

// loop through the users returned by the query
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
  // make an array for this user with the basic data
  $user = array('id' => $row['user_id'], 'name' => $row['name']);

  // add fb data
  $user['fb'] = $pdo->prepare('select * from fb where user_id = :user_id')
    ->execute(array(':user_id' => $user['id']))
    ->fetch(PDO::FETCH_ASSOC);


  // add li data
  $user['li'] = $pdo->prepare('select * from li where user_id = :user_id')
    ->execute(array(':user_id' => $user['id']))
    ->fetch(PDO::FETCH_ASSOC);

  // add tw data
  $user['tw'] = $pdo->prepare('select * from tw where user_id = :user_id')
    ->execute(array(':user_id' => $user['id']))
    ->fetch(PDO::FETCH_ASSOC);

  // add new user to the full array
  $users[] = $user;
}

// do stuff with the $users array

答案 1 :(得分:1)

所以你可以做类似

的事情
SELECT u.ID, u.Name, s.* FROM users u
INNER JOIN socialNetworks s ON u.ID = s.user_id
ORDER BY u.ID

循环遍历相同的ID,为每个用户构建数组:

$id = -1;
$i = 0;
$userArr = array();
while($row = mysqli_fetch_array($res))
{
    //parse row set user/SN info

    if($id != $row["ID"])
    {
        $tempUserArr = array();
        $id = $row["ID"];
        //set user and sn info to $tempUserArr
        $userArr[] = $tempUserArr;
    }
    else
    {
        $tempSN_arr = array();
        //set social network info to $tempSN_arr
        $userArr[$i][$row["sn_type"]] = $tempSN_arr;
    }
    $i++;
}

答案 2 :(得分:1)

$sql = "SELECT * FROM user";
$rows = mysql_query($sql);


$user_array = array()

while($record = mysql_fetch_array($orws){

   $new_array = array();
   $new_array['name']  = $record['name'];
   $new_array['id']  =$record['user_id'];
   $new_array['facebook'] = array(); //function or query to fill data
   $new_array['twitter'] = array();//function or query to fill data
   $new_array['linkedin'] = array();//function or query to fill data

     $user_array[]=$new_array;
}
print_r($user_array);

答案 3 :(得分:1)

作为我的另一个答案的替代方案,这个将在一个查询中获取所有数据:

select
  users.id,
  users.name,
  fb.account_url as 'facebook_account_url',
  li.account_url as 'linkedin_account_url',
  tw.account_url as 'twitter_account_url',
from
  users
  inner join social_network as fb on users.id = fb.user_id and fb.social_network = 'fb'
  inner join social_network as tw on users.id = tw.user_id and tw.social_network = 'tw'
  inner join social_network as li on users.id = li.user_id and li.social_network = 'li'

结果数据不会像问题中显示的那样是多维数组,但它将在一个MySQL查询中完成。