php - SQL 查询来查找特定博客文章的作者?

标签 php mysql database

您好,我正在尝试从我的数据库中提取某篇博客文章的作者用户的名字,但遇到了一些问题。我真的对如何将两个表连接在一起感到困惑,并且无法真正找到简单的解释。这是我的两个表:

CREATE TABLE IF NOT EXISTS `goldhub_user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(20) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` char(40) NOT NULL,
  `first_name` varchar(20) NOT NULL,
  `last_name` varchar(20) NOT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `goldhub_email` (`email`)
)

CREATE TABLE IF NOT EXISTS `goldhub_post` (
  `post_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `category_id` int(11) DEFAULT NULL,
  `post_content` varchar(255) DEFAULT NULL,
  `post_date` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`post_id`),
  KEY `user_id` (`user_id`),
  KEY `category_id` (`category_id`)
)

任何指导将不胜感激。

最佳答案

SELECT goldhub_user.first_name FROM goldhub_post
INNER JOIN goldhub_user ON goldhub_user.user_id = goldhub_post.user_id
WHERE goldhub_post.post_id = ?

? 替换为帖子 ID。

关于php - SQL 查询来查找特定博客文章的作者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30492058/

相关文章:

java - WhichdDatabase 将被证明是有效的?

php - 尝试将 JSON 数据传递给 Highcharts 但没有成功

php - session 变得疯狂。只有 Mozilla 能够处理

python - django.db.utils.operationalError : (2059 ,"Authentication Plugin ' caching_sha2_password'")

database - 在 postgres 中交换生产模式

c# - C# 将Excel中的数据导入数据库

php - SQL错误: Invalid parameter number: parameter was not defined

php - 非常简单的 MySQL 查询不起作用

mysql delete with select multi conditions

mysql - 查找MySQL中的重复记录2