简体   繁体   English

连接表,其中第二个表中多次包含第一个表主键

[英]Join table where first table primary key is contained multiple times in second table

I have a little doubts. 我有点怀疑。

Is it possible to done this with mysql or i have to do another query while after i get result from user table. 是否有可能用mysql完成此操作,或者在我从用户表中获取结果后必须再次查询。

I have table users 我有桌子的用户

id name 身份证名

I have table phonenumbers 我有桌子上的电话号码

phonenumberid userid and phone phonenumberid用户ID和电话

A user can have multiple phone numbers which will be stored in phonenumbers table with foreign key userid . 一个用户可以有多个电话号码 ,这些电话号码将与外键userid一起存储在phonenumbers表中。

Example. 例。 If the user will be on edit personal info page and there should be display all phone numbers which he have inserted to delete them to edit etc... 如果用户将在编辑个人信息页面上并且应该显示他已插入以删除它们以进行编辑等的所有电话号码...

So how can i get all phonenumers in one query (WHERE users.id = 1); 因此,如何在一个查询中获取所有电话号码(WHERE users.id = 1);

Or i should select first basic informations in the users table and then make another separated query to get and store in array userphonenumbers or push in the user table result array. 或者我应该先在用户表中选择基本信息,然后再进行另一个单独的查询,以获取并存储在用户电话号码数组中或在用户表结果数组中推送。

I hope that we will get with good examples. 我希望我们能举例说明。

EDIT : 编辑:

Here is example query 这是查询示例

SELECT users.id, phonenumbers.userid, phone, name 
FROM users 
LEFT JOIN phonenumbers ON phonenumbers.userid = users.id 
WHERE users.id = 1;

I have 2 phonenumbers in phonenumbers table with foreignkey(userid) = 1 But when I make the query I got duplicate rows with the same name and when I do the foreach loop I will get duplicates. 我的电话号码表中有2个电话号码,其中Foreignkey(userid)= 1,但是当我进行查询时,我得到了具有相同名称的重复行,而当我执行foreach循环时,我将获得重复。

I want them to be grouped like for userid = 1 have phones 545454 and 324398493 我希望将它们像userid = 1一样进行分组,有电话545454和324398493

Result Example 结果示例

id     name   phone     userid
1      John   4112258     1
1      John   5698745     1

If user have 10 phones, 10 additional rows will be added and in the loop all thoose rows will be repeated, but its not necessary, will be acted like multiple users 如果用户有10个电话,则会添加10个附加行,并且在循环中将重复所有thoose行,但并非必须如此,其作用类似于多个用户

With relational algebra based approach you can not get from a select performing the Cartesian product of two tables by connecting the user id that you provide an answer for every customer a single line with a number of variables potentially phone numbers. 使用基于关系代数的方法时,您无法通过将用户ID连接到为每位客户提供答案的用户ID的一行中,而该行具有多个变量(可能是电话号码),从而无法选择执行两个表的笛卡尔积。

If you want to get a result like this the easiest way is to use a procedure that carries out a query on just user and then recursively for each user perform a subquery on telephone numbers on this result can intervene to format the result as you want 如果要获得这样的结果,最简单的方法是使用对仅用户执行查询的过程,然后递归地为每个用户对电话号码执行子查询,以便对该结果进行干预以根据需要设置结果的格式

You may find this useful? 您可能会发现这有用吗?

connection.php connection.php

function db(){
  return new mysqli('host', 'username', 'password', 'database');
}

page.php page.php

include_once 'connection.php'; $db = db(); $a = array();
if($uq = $db->query('SELECT id,name FROM users')){
  if($uq->num_rows > 0){
    while($u = $uq->fetch_object()){
      $u->phones = array();
      if($q = $db->query("SELECT phonenumberid,phone FROM phonenumbers WHERE userid={$u->id}")){
        if($q->num_rows > 0){
          while($o = $q->fetch_object()){
            $u->phones[] = $o;
          }
        }
        $q->free();
      }
      $a[] = $u;
    }
  }
  $uq->free();
}
print_r($a);
$db->close();

I can't see where your SQL result is wrong. 我看不到您的SQL结果在哪里。 It is doing what it is supposed to do. 它正在做应该做的事情。 It sounds like you want it to look like a human readable report. 听起来您想让它看起来像是人类可读的报告。 You have to do more than the SQL, if that is the case. 在这种情况下,您需要做的不仅仅是SQL。 Is that what you mean? 你是这个意思吗?

Modified from what I originally modified from somewhere on SO ( $obj was passed to my view here): 从我最初在SO某处进行的修改( $obj在这里传递到我的视图):

<?PHP   if (!empty($obj)): 
          $previous_heading = false;
?>

          some_markup like <table>

    <?PHP foreach ($obj as $key => $value): ?>

    <tr>                

    <?PHP if (!$previous_heading || $value['user.id'] != $previous_heading): ?>

               Do markup stuff with the heading like display the name of the user

            <td>
             <?PHP $value['my_column'];
            </td>

    <?PHP end if; ?>
                    ..more markup...
         <td>
             <?PHP $value['phone_number'];
         </td>
        </tr>

    <?PHP 
              //do other stuff that is not related to your heading, "John," like display   phone numbers.
           $previous_heading = $value['user.id'];
           end foreach;
         ?>
        </table>
        ...ending markup tags....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM