dongzhong1891 2014-08-13 19:12
浏览 31
已采纳

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

In my database, I have 3 tables.

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.
  • Assign the category_id of a joke to the name and display it
  • 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):

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

  • 写回答

1条回答 默认 最新

  • douji9816 2014-08-13 19:19
    关注

    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.

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)