简体   繁体   English

如何在MySQL上联接2个表?

[英]How to JOIN 2 tables on mySql?

I have two tables: publick_feed and users 我有两个表: publick_feedusers

I want to SELECT all from public_feed and also SELECT a three columns from users whose id is the same of user_id in public_feed and assign the rows returned from public_feed to the column in users table ( correspondent) 我想选择所有从public_feed,还可以选择从用户ID三列是一样的public_feeduser_id并指定行在用户表public_feed到列返回(通讯员)

I try this: 我尝试这样:

<?php

$sql = "
SELECT * FROM public_feed
WHERE user_id IN
(SELECT id FROM users) AND 
(SELECT Firstname,Lastname,Avatar FROM users WHERE id IN(SELECT user_id FROM public_feed))  

";

$query  = mysqli_query($dbc_conn,$sql);
if(mysqli_num_rows($query) > 0){
    while($row = mysqli_fetch_assoc($query)){
        //echo rows with correspondent details from the users table
        echo $row['user_id'];
        }
}

<?

Please any help will be much appreciated. 请任何帮助将不胜感激。 Thank you. 谢谢。

I think doing a join would be cleaner than using a complicated subquery: 我认为进行联接比使用复杂的子查询更干净:

SELECT u.Firstname,
       u.Lastname,
       u.Avatar,
       COALESCE(pf.User_id, 'NA'),
       COALESCE(pf.Post, 'NA'),
       COALESCE(pf.Date, 'NA')
FROM users u
LEFT JOIN public_feed pf
    ON u.Id = pf.User_id

I chose a LEFT JOIN of users against public_feed on the assumption that every feed will have an entry in the users table, but not necessarily vice-versa. 我假设每个提要都会在users表中有一个条目,但相对于public_feed选择了一个LEFT JOIN users For those users who have no feed entries, NA would appear in those columns and that user would appear in only a single record. 对于那些没有提要条目的用户, NA将出现在这些列中,而该用户将仅出现在单个记录中。

Or version with left join in case if there is no user in public_feed, and you still want to fetch user data 或者,如果public_feed中没有用户,而您仍想获取用户数据,则使用左连接版本

SELECT
  u.*, f.* 
FROM
  public_feed f LEFT JOIN
  users u ON f.user_id = u.id;

Because author asked for explanation, here it is: 因为作者要求说明,所以在这里是:

First we are going to use table name alias to make query shorter 首先,我们将使用表名别名来简化查询

public_feed f

and

users u

we are saying that want to refer to tables with an alias. 我们说的是要引用具有别名的表。 Of course * means that we want to select all columns 当然*表示我们要选择所有列

SELECT users.*, public_feed.*

is equal to 等于

SELECT u.*, f.*

Of course you can use any other letters as an alias 当然,您可以使用其他任何字母作为别名

Next we are saying that public_feed.user_id must be equal to users.id. 接下来,我们要说public_feed.user_id必须等于users.id。 But when public feed entry does not exists just display columns with null values. 但是,当公共提要条目不存在时,仅显示具有空值的列。 This is why we are using LEFT JOIN instead of INNER JOIN. 这就是为什么我们使用LEFT JOIN而不是INNER JOIN的原因。 In general JOINS are used to fetch related data from more than one related tables. 通常,JOINS用于从多个相关表中获取相关数据。

ON keyword is saying values from which columns in the tables must be equal to satisfy the request ON关键字表示值,表中的列必须等于该值才能满足请求

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

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