简体   繁体   中英

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.

I have table users

id name

I have table phonenumbers

phonenumberid userid and phone

A user can have multiple phone numbers which will be stored in phonenumbers table with foreign key userid .

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);

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.

I want them to be grouped like for userid = 1 have phones 545454 and 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

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.

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

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

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. 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. Is that what you mean?

Modified from what I originally modified from somewhere on SO ( $obj was passed to my view here):

<?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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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