1

我有两张桌子,一张是“用户”,一张是“社交网络”。我想以某种格式从两者中获取数据,并且社交网络表将 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、姓名、图片等

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

我要获取的是用户表中的整行和社交网络表中的每一行。我宁愿在单个查询中执行此操作。

4

4 回答 4

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
于 2012-06-20T21:01:52.770 回答
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++;
}
于 2012-06-20T20:57:21.610 回答
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);
于 2012-06-20T20:57:32.007 回答
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 查询中完成。

于 2012-06-20T21:13:41.157 回答