简体   繁体   English

使用查询(SQL)从所有三个表中获取数据

[英]Getting data from all three tables using a query (SQL)

In my database, I have 3 tables. 在我的数据库中,我有3个表。

Jokes: 笑话:

CREATE TABLE IF NOT EXISTS `jokes` (
  `joke_id` int(11) NOT NULL AUTO_INCREMENT,
  `joke` varchar(1024) NOT NULL,
  `category_id` int(11) NOT NULL,
  `vote` int(255) NOT NULL,
  `date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Category: 类别:

CREATE TABLE IF NOT EXISTS `category` (
  `category_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(51) NOT NULL,
  PRIMARY KEY (`category_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

And finally, Comments: 最后,评论:

CREATE TABLE IF NOT EXISTS `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user` varchar(40) NOT NULL,
  `comment` text NOT NULL,
  `joke_id` int(11) NOT NULL,
  `post_id` int(11) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

These three tables need to be joined together to get data from each. 这三个表需要连接在一起才能从每个表中获取数据。 So far i have used these three queries with no luck in getting what i need. 到目前为止,我在运气不好的情况下使用了这三个查询。

what i need this query to do is 我需要这个查询来做的是

  • Assign the joke_id to the joke_id in the comments, and also displays the joke_id in the view. 在注释中将joke_id分配给joke_id,并在视图中显示joke_id。
  • Assign the category_id of a joke to the name and display it 将笑话的category_id分配给名称并显示它
  • grab the votes 抢票
  • grab the jokes 抓住笑话

1st query - this query does actually grab all the data i want, but at the same time does not grab the joke_id and pass it to view (as i need the joke_id to assign comments to that unique id): 第一个查询-该查询实际上确实获取了我想要的所有数据,但同时没有获取joke_id并将其传递给视图(因为我需要joke_id将注释分配给该唯一ID):

SELECT j.*, c.name, 
co.* FROM jokes j LEFT 
JOIN category c ON c.category_id 
= j.category_id LEFT JOIN 
comments co ON co.joke_id = 
j.joke_id WHERE j.joke_id = '$joke_id'

2nd query - This query joins the category to the correct one, and displays the joke and joke_id for me to assign comments to. 第二个查询-此查询将类别连接到正确的类别,并显示笑话和joke_id供我分配注释。 But it does not show any comments 但是它没有显示任何评论

SELECT j.*, c.name 
FROM jokes j LEFT JOIN category c 
ON c.category_id = j.category_id 
WHERE joke_id = '$joke_id'

3rd query - This query was provided by a user on stack overflow, but seems to throw a tonne of errors my way when a there is no comment attatched to that joke 第三个查询-该查询是由用户在堆栈溢出时提供的,但当笑话没有注释时,似乎会抛出大量错误

SELECT c.*, j.*, co.*
FROM jokes j
INNER JOIN category c ON c.category_id = j.category_id
INNER JOIN comments co ON co.joke_id = j.joke_id
WHERE j.joke_id =  '$joke_id'

Any help altering this query to get all the items in the three database together would be much appreciated! 任何帮助更改此查询以将三个数据库中的所有项目汇总在一起的帮助将不胜感激!

Edit the selected fields to suits your needs. 编辑所选字段以适合您的需求。 Also if you want to display only one joke and its data you have to set the id of the joke you want as you did on the WHERE for instance. 另外,如果只想显示一个笑话及其数据,就必须像在WHERE上一样设置想要笑话的ID。

SELECT jokes.*, category.*, comments.* FROM jokes LEFT JOIN category ON jokes.category_id = category.category_id LEFT JOIN comments ON jokes.jokes_id = comments.joke_id

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

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