简体   繁体   English

MySQL如何显示两个表中的数据

[英]MySQL how to display data from two tables

I'm trying to display the username of the person who has submitted the most articles but I don't know how to do it using MySQL & PHP, can someone help me? 我正在尝试显示提交文章最多的人的用户名,但我不知道如何使用MySQL和PHP进行操作,有人可以帮助我吗?

Here is the MySQL code. 这是MySQL代码。

CREATE TABLE users (
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(255) DEFAULT NULL,
pass CHAR(40) NOT NULL,
PRIMARY KEY (user_id)
);

CREATE TABLE users_articles (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED mNOT NULL,
title TEXT NOT NULL,
acontent LONGTEXT NOT NULL,
PRIMARY KEY (id)
);

Here is the code I have so far. 这是我到目前为止的代码。

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT COUNT(*) as coun, user_id 
                             FROM users_articles 
                             GROUP BY user_id 
                             ORDER BY coun DESC
                             LIMIT 1");

If you want to get the user's name, you should use the next query: 如果要获取用户名,则应使用下一个查询:

SELECT users.name, COUNT(users_articles.id) AS coun 
FROM users_articles
LEFT JOIN users_articles ON users.id=users_articles.user_id
GROUP BY users_articles.user_id
ORDER BY coun DESC
LIMIT 1

SELECT COUNT(*) as coun, user_id , users.username SELECT COUNT(*) as coun, user_id , users.username SELECT COUNT(*) as coun, user_id , users.username
FROM users_articles , users FROM users_articlesusers
WHERE users_articles.user_id = users.user_id
GROUP BY user_id
ORDER BY coun DESC
LIMIT 1

What you want to do is a join . 您想要做的是加入

The SQL query you need is this: 您需要的SQL查询是这样的:

SELECT COUNT(*) as coun, users.user_id, username
FROM users_articles
INNER JOIN users
ON users_articles.user_id = users.user_id
GROUP BY user_id
ORDER BY coun DESC
LIMIT 1

I tested this and it works. 我对此进行了测试,并且有效。
The result table contains the number of articles of the user, its user id and its username. 结果表包含用户的文章数,用户ID和用户名。

select u.user_id, count(ua.id) as num_articles from users u left outer join users_articles ua on u.user_id = ua.user_id group by u.user_id order by num_articles desc 从用户中选择u.user_id,count(ua.id)作为num_articles在u.user_id = ua.user_id上外部加入users_articles ua按u.user_id分组,按num_articles desc排序

The left outer join (as opposed to an inner join) ensures that all users are represented in the result, no matter if they have a record in users_articles or not. 左外部users_articles (而不是users_articles )确保所有用户都在结果中表示,无论他们是否在users_articles都有记录。

EDIT: Since you only want the person who has submitted the most articles, you do not necessarily need the left outer join (as long as there is at least one user who has written any articles). 编辑:由于您只希望提交文章最多的人,所以您不一定需要左外部联接(只要有至少一位撰写任何文章的用户)。 For a complete list, it would be useful, however. 对于完整列表,这将很有用。

无论是上述怪胎给出的哪种查询,您都只使用“不要忘记”选择查询中的“ 用户名 ”字段,因为它们都不包含用户名字段

use like this, 这样使用

SELECT COUNT(users_articles.*) as coun, users_articles.user_id, users.username 
FROM users_articles, users
WHERE users_articles.user_id = users.user_id
GROUP BY users.user_id
ORDER BY coun DESC

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

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