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,
`name` 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 ;
This function is stored in my read controller, with the name of Joke and it grabs the data from the db then sends it to the view.
public function joke($joke_id = FALSE)
{
if ($joke_id === FALSE) redirect('main'); //go to default controller
$data['results'] = $this->comments_m->getComments($joke_id, $this->uri->segment(2));
$this->load->view('template/header');
$this->load->view('template/sidebar');
$this->load->view('content/read', $data);
$this->load->view('template/footer');
}
The model (getjokes_m) has a function called readJokes which gets the joke, joke_id, category name, votes, comments. This function is used to read specific jokes in the database.
function readJokes($joke_id)
{
$query = $this->db->query("SELECT c.name, j.*, 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'") or die("No results found" );
//displays the results
return $query->result();
}
The query above is where the problem lies. For the moment it returns the joke as many times as the joke has been commented. I.e if the joke has 6 comments, the joke will show 6 times (no idea why). Any help altering this query, to just show the joke once, with all the comments the joke has, will be much appreciated.
You want a group by with group_concat() for the comments:
SELECT c.name, j.*, group_concat(co.comment separator '|') as comments
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'
GROUP BY j.joke_id;
Actually the group by is not strictly needed here, unless you want to get the results for more than one joke.
Related
I am trying to show the amount of reviews for each restaurant.
I made 2 tables containing my data.
CREATE TABLE `restaurants` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`city` varchar(200) NOT NULL,
`country` varchar(200) NOT NULL,
`score` int(1) NOT NULL DEFAULT '0',
`reviews` int(1) NOT NULL DEFAULT '0',
`slug` varchar(200) NOT NULL DEFAULT 'slug-test',
`approved` int(1) NOT NULL DEFAULT '0',
`description` text NOT NULL,
`review` int(11) DEFAULT '0',
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`img_url` varchar(255) NOT NULL,
`category` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=latin1
CREATE TABLE `reviews` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`restaurant_id` int(11) NOT NULL,
`review_text` text NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `restaurant_id` (`restaurant_id`),
CONSTRAINT `reviews_ibfk_1` FOREIGN KEY (`restaurant_id`) REFERENCES `restaurants` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
I am already showing my restaurants in a list.
$query = "SELECT * FROM restaurants WHERE approved = 1 ORDER BY created_date DESC";
$result = $mysqli->query($query);
while($row = $result->fetch_array()) {
**HTML WITH MY VARIABLES**
}
This is how u currently show my reviews (hardcoded result in my restaurants table.
if ($reviews <= "0") {
echo "<a href='#' title='Write reviews'><p class='purple-def-color fz-12 mb-0'>write a review</p></a>";
} else if ($reviews > "1") {
echo "<p class='purple-def-color mb-0'>". $row['reviews'] ." reviews</p>";
} else {
echo "<p class='purple-def-color mb-0'>" . $row['reviews'] . " review</p>";
}
I tried to use a JOIN query but was not successful to show the amount of reviews in any way.
Based on your table structure, you can get a review count for each restaurant by joining the reviews table, selecting the COUNT of reviews, and grouping by restaurant ID.
Grouping by restaurant allows you to get an aggregate (e.g. count) of the joined review records for each restaurant.
SELECT
rs.*,
COUNT(rv.`id`) as `reviewCount`
FROM `restaurants` rs
LEFT JOIN `reviews` rv
ON (rv.`restaurant_id` = rs.`id`)
WHERE rs.`approved` = 1
GROUP BY rs.`id`
ORDER BY rs.`created_date` DESC;
Then, when you fetch the rows in PHP, you can reference each row's review count:
echo $row['reviewCount'];
To further demonstrate aggregate functions, here's an example of how to select the average, minimum, and maximum review score for each restaurant:
SELECT
rs.*,
COUNT(rv.`id`) as `reviewCount`,
AVG(rv.`score`) as `reviewAverageScore`,
MIN(rv.`score`) as `reviewMinScore`,
MAX(rv.`score`) as `reviewMaxScore`
FROM `restaurants` rs
LEFT JOIN `reviews` rv
ON (rv.`restaurant_id` = rs.`id`)
WHERE rs.`approved` = 1
GROUP BY rs.`id`
ORDER BY rs.`created_date` DESC;
Try a left join:
$query = 'SELECT * FROM restaurants LEFT JOIN reviews on reviews.restaurant_id = restaurants.id'
A left join returns all records from the left table (restaurants), and the matched records from the right table (reviews)
So the idea is to get information from 3 tables:
R_Regle: rule table, basic info of the rule
C_Commentaire: the number of comments this rule has
I_Interet: The SUM of all upvotes and downvotes from one rule.
The structure of my I_Interet:
I_ID / I_Idregle / I_Idpseudo / I_Upvote / I_Downvote
Each row can only contain 1 upvote or 1 downvote.
so with this request:
SELECT R_Id, R_Date, R_Titre, R_Contenu, R_Etape, COUNT(C_Commentaire.C_IdRegle) AS CountComment, SUM(I_Interet.I_Up) AS Up ,SUM(I_Interet.I_Down) AS Down
FROM R_Regle
LEFT JOIN C_Commentaire ON C_Commentaire.C_IdRegle = R_Regle.R_Id
LEFT JOIN I_Interet ON I_Interet.I_IdRegle = R_Regle.R_Id
GROUP BY R_ID
I try to obtain all those information.
Problem is:
COUNT(C_Commentaire.C_IdRegle) AS CountComment
Gives me the count of all COMMENTS and UPVOTES/DOWNVOTES.
So if there are 4 comments and 3 downvotes, it will count it like that
$row['CountComment'] = 7;
But obviously, I don't want that. I want
$row['CountComment'] = 4;
Any ideas?
EDIT 1:
Here are the tables:
R_REGLE
CREATE TABLE `R_Regle` (
`R_Id` int(11) NOT NULL AUTO_INCREMENT,
`R_Auteur` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`R_Date` date NOT NULL,
`R_Titre` varchar(150) CHARACTER SET latin1 NOT NULL,
`R_Contenu` text CHARACTER SET latin1 NOT NULL,
`R_Etape` int(11) NOT NULL DEFAULT '1' COMMENT '1 = discussion 2= VOTE',
PRIMARY KEY (`R_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
C_COMMENTAIRE
CREATE TABLE `C_Commentaire` (
`C_Id` int(11) NOT NULL AUTO_INCREMENT,
`C_Date` datetime NOT NULL,
`C_Pseudo` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`C_Contenu` text COLLATE utf8_unicode_ci NOT NULL,
`C_IdRegle` int(11) NOT NULL,
PRIMARY KEY (`C_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I_INTERET
CREATE TABLE `I_Interet` (
`I_Id` int(11) NOT NULL AUTO_INCREMENT,
`I_IdRegle` int(11) NOT NULL,
`I_Pseudo` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`I_Up` tinyint(1) NOT NULL DEFAULT '0',
`I_Down` tinyint(1) DEFAULT '0',
PRIMARY KEY (`I_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I can suggest using correlated query.. not sure if thats what you want since you didn't post any table structures its hard to know :
SELECT R_Id, R_Date, R_Titre, R_Contenu, R_Etape,
(select COUNT(*) from C_Commentaire where C_Commentaire.C_IdRegle = R_Regle.R_Id) AS CountComment,
SUM(I_Interet.I_Up) AS Up ,
SUM(I_Interet.I_Down) AS Down
FROM R_Regle
LEFT JOIN I_Interet ON I_Interet.I_IdRegle = R_Regle.R_Id
GROUP BY R_ID
Write your query as below:
SELECT R_Regle.R_Id, R_Regle.R_Date, R_Regle.R_Titre, R_Regle.R_Contenu, R_Regle.R_Etape,
COUNT(C_Commentaire.C_IdRegle) AS CommentCounts,
SUM(I_Interet.I_Up) AS Up,
SUM(I_Interet.I_Down) AS Down
FROM R_Regle
LEFT JOIN C_Commentaire ON R_Regle.R_Id = C_Commentaire.C_IdRegle
LEFT JOIN I_Interet ON R_Regle.R_Id = I_Interet.I_IdRegle
GROUP BY R_Regle.R_ID
In your query there are duplicate records arisen when you make a left join among the three tables.
The following query is one of few ways to achieve your desired result.
SELECT
t.*,
SUM(I_Interet.I_Up) AS Up,
SUM(I_Interet.I_Down) AS Down
FROM
(
SELECT
R_Id,
R_Date,
R_Titre,
R_Contenu,
R_Etape,
COUNT(c_commentaire.C_IdRegle) countComment
FROM
R_Regle LEFT JOIN C_Commentaire ON C_Commentaire.C_IdRegle = R_Regle.R_Id
GROUP BY R_Regle.R_Id ) t
LEFT JOIN I_Interet ON I_Interet.I_IdRegle = t.R_Id
GROUP BY t.R_Id
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!
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
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,
`name` 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 ;
I need to join all three tables together to get the category name of a joke, the joke itself, and the unique comments correlating to that specific joke_id.
For the moment I can only join two tables where the joke_id = 1. This will return the comments for that joke.
This function is stored in my read controller, with the name of Joke:
public function joke($joke_id = FALSE)
{
if ($joke_id === FALSE) redirect('main'); //go to default controller
$data['results'] = $this->comments_m->getComments($joke_id, $this->uri->segment(2));
$this->load->view('template/header');
$this->load->view('template/sidebar');
$this->load->view('content/read', $data);
$this->load->view('template/footer');
}
The model (comments_m) has a function called getComments which grabs the comments correlating to joke_id = 1:
//gets the comments
function getComments (){
//joke id should grab the id of the joke which was commented on
$joke_id = 1;
$query = $this->db->query("SELECT jokes.*, comments.* FROM jokes LEFT OUTER JOIN comments ON jokes.joke_id = comments.joke_id WHERE jokes.joke_id = '$joke_id'");
return $query->result();
}
I need a SQL query in the getComments function which grabs the category name, the joke, the joke_id, comment and comment_id. How can I alter my query to return the information that I nee?
Try this query
SELECT jokes.*,category.*,comments.* from jokes
INNER JOIN category ON jokes.category_id=category.category_id
INNER JOIN comments ON jokes.joke_id=comments.joke_id
where jokes.joke_id = '$joke_id';
I need a little help for one sql query;
here is my basic user database table
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(100) NOT NULL,
`pass` varchar(255) NOT NULL,
`fname` varchar(50) NOT NULL,
`lname` varchar(40) NOT NULL,
`network` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;
and here is my basic topics database table
CREATE TABLE IF NOT EXISTS `topics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` varchar(100) NOT NULL,
`title` varchar(255) NOT NULL,
`body` text,
`tags` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
What I'm trying to do is;
i.e. list all topics from users who joined "harvard" network
You just need a simple JOIN. Join the tables on the userID (of those who are in "harvard").
SELECT title,body
FROM topics, users
WHERE userid = users.id
AND network = 'harvard'
Or using the JOIN keyword:
SELECT title,body
FROM topics
JOIN users ON userid = users.id
WHERE network = 'harvard'
This should do the trick, I think:
SELECT
title
FROM
topics t
INNER JOIN
users u ON t.userid=u.id
WHERE
network='Harvard'
SELECT t.title
FROM users u
INNER JOIN topics t ON u.id = t.userid
WHERE u.network = 'harvard'
GROUP BY u.id