There is two table: user, comments
The structure is like this:
user
-----
id
name
comments
--------
id
title
user_id
comments_id
Notice that the comments_id is the parent ID of that comment record, that means, if comments_id is null, it is a POST, if it is not null, it is a REPLY.
So I would like to select them and ordering like this
Post 1
Reply 1 for Post 1
Reply 2 for Post 1
Post2
...
And here is the codeigniter query I have attempt, I also need the user table for getting the user name
$this->db->select('c.id as id, u.name as username, c.txtMsg as txtMsg, c.createDate as createDate, c.updateDate as updateDate, c.imageURL as imageURL, c.postID as postID');
$this->db->from('comment as c, user as u');
$this->db->where('u.id = c.userID');
$this->db->order_by('c.id', $order_type);
How to construct the query to order the list? Thanks for helping.
Related
I have a two tables in my database. One is "blog" second is "comment".
Blog table have this structure
id, title, text, writer_id, created_at, updated_at
Comment table have this structure
id, blog_id, text, commenter_id, created_at, updated_at
I want to get id value from blog table as in comment table in the form of blog_id . How can i get the same value.
Example:
One blog posted. blog table have these values.
id: 1
title: hi i m blogger
text: how are you
writer_id: 5 (same User id)
created_at:25 feb
updated_at: 25 feb
Blog posted . One commenter comes and comment on this post. Value should be comes as like this in comment table
id=1
blog_id:1
text: ok i know
created_at:25 feb
updated_at: 25 feb
id of blog table and blog_id of comment table should be same. How can i do this with query or php code?.
Try This Query
SELECT b.id, c.`text`,..... FROM blog b
LEFT JOIN comment c
ON b.id = c.blog_id
ORDER BY commenter_id DESC ;
if you want for perticular blog so add WHERE CONDITION
SELECT b.id, c.`text`,.... FROM blog b
LEFT JOIN comment c
ON b.id = c.blog_id
b.id = 'Your ID'
ORDER BY commenter_id DESC ;
First of all you have to put foreign constraint that the value of column "Blog_Id" in table comment should always be from table "blog"
select * from blog bg inner join comment cm on cm.blog_id = blog.id this is the query for retrieving what you want.
I want to display the number of posts a user has made, but I have a few different tables and need to match ids and count them.
Something like this:
Users Table
userid username email regdate
34 mister email#some.tld 2013-10-26 12:01:07
Posts Table
postid creator post_comment post_title status
1 34 This is comment Post 1 published
2 12 This is comment Post 2 published
3 34 This is comment Post 3 pending
4 25 This is comment Post 4 published
5 34 This is comment Post 5 published
Now I already have a query where I select all data about a particular user from the users table:
mysqli_query($conn, "SELECT * FROM users WHERE userid = $userid");
$userid is the id of user that's currently logged in. And this works fine: it selects all relevant information about that user.
But I want to display the number of posts each user makes. I read somewhere on w3schools the SQL COUNT function works something like this:
mysqli_query($conn, "SELECT COUNT(creator) AS userposts FROM posts WHERE creator=$userid");
However, as you can see in table example posted, out of 5 posts, 3 are from user 34 and 1 of these 3 is not published.
I don't know how to make a SQL query that selects all users where userid matches $userid and then selects creator and status from posts and counts the number of posts where creator is $userid and status is published.
This is the output I'm ultimately aiming for:
User mister have 2 posts published.
In order to get only the number of published posts, you should refactor your condition statement by ANDing status = 'published'
mysqli_query($conn, "SELECT COUNT(creator) AS num_posts FROM posts
WHERE creator=$userid AND status = 'published'");
SELECT COUNT(creator) AS userposts FROM posts p
LEFT JOIN users u ON p.creator=u.userid
WHERE creator=$userid AND status='published'
As you asked in the comment, all in one query:
SELECT users.*, COUNT(creator) AS userposts FROM users u
LEFT JOIN posts p ON p.creator = u.userid
WHERE p.creator = $userid AND p.status = 'published'
There might be an easy solution here, but it seems to have me tripped up. I'm trying to query a table based on an array of values in two columns. Here is the pertinent table structure and sample data
comment table
id, userId, articleId .... etc etc
article table
id, userId .... etc etc
Data: UserIds = 3, 10. ArticleIds = 1, 2
Let's say I'm trying to find all the comments for a particular set of article IDs: 1,2
I can easily use this query
select * from comments WHERE articleId IN(1,2)
However, here is where it gets complex. I have a query that executes prior to the comments query that determines the appropriate article IDs. Those IDs are in an array. Also in an array are the corresponding user IDs for each article.
What I want to do now is query the comments table for only the articles in the array (1,2) AND only for those comments made by the original author (3, 10).
The simple query above will bring back all the comments for articleId 1 and 20. So for example I can't figure out where to add another conditional that says onyl comments for articleId 1, 20 AND corresponding userId, 3, 10.
Any help or suggestions would be much appreciated!
I think the simplest way is to write:
SELECT comments.*
FROM articles
JOIN comments
ON articles.id = comments.articleId
AND articles.userId = comments.userId
WHERE articles.id IN (1, 2)
;
The AND articles.userId = comments.userId clause is what enforces your "only for those comments made by the original author" requirement.
Alternatively, you can use an EXISTS clause:
SELECT *
FROM comments
WHERE articleId IN (1, 2)
AND EXISTS
( SELECT 1
FROM articles
WHERE id = comments.articleId
AND userId = comments.userId
)
;
or a single-row subquery:
SELECT *
FROM comments
WHERE articleId IN (1, 2)
AND userId =
( SELECT userId
FROM articles
WHERE id = comments.articleId
)
;
Have you tried just
select * from comments WHERE articleId IN(1,2) and authorId in (3,10)
If not, please update your question why it's so.
You can add as many IN statements as you want:
select * from comments WHERE articleId IN(1,2) AND userId IN (3,10)
Can't you just make the query that runs first a subquery within your stated query:
SELECT * FROM `comments` WHERE `articleId` IN(1,2) AND `userId` IN (__subquery__)
-- you can use subquery
select * from comments WHERE articleId IN(1,2)
and userId in ( select userId from article where id in (1,2) )
I'm having a bit of trouble figuring out an SQL statement that I need to make.
I have 2 tables
Friends
===================================
friendfromid | friendtoid | request
===================================
Users
=================
userid | username
=================
I need to get the userid and username of each user that is my friend.
My id is either in the friendfromid or friendtoid depending on if I am requested the user or the user requested me.
So basically what needs to happen is the script needs to look at the friends table, get all the rows where my id is either friendfromid or friendtoid check that the request field is set to 1, take all the rows that fit that match then get the ids and usernames of each friendfromid or friendtoid which isn't mine.
For instance, if my id was 8 and the friends id was 9, let's say they requested me their id would be in the friendfromid field and my id would be in the friendtoid field, that means the script would take their id (9) and match it to that user in the users table.
This will give friends list with friend id and friend name:
SELECT u.userid , u.username
FROM Friends f
JOIN Users u
ON (( f.friendfromid ={the ID} AND friendtoid=u.userid)
OR ( f.friendtoid = {the ID} AND friendfromid=u.userid))
WHERE f.request = 1
Put {the ID}= user id whose friend list required.
Should be something like this:
SELECT *
FROM users
JOIN friends
ON friendfromid != userid
OR friendtoid != userid
WHERE request = 1
AND userid = {the ID}
Can't achieve in 1 query.
To get friendtoid with "my user ID" in friendfromid field:
SELECT b.friendtoid FROM Users a, Friends b WHERE request=1 AND friendfromid = a.userid
Vice versa for friendfromid.
Use a UNION query, joining to friends as below
SELECT username FROM user INNER JOIN friends ON (userid = friendfrom) WHERE friendto = {myid) AND request = 1
UNION
SELECT username FROM user INNER JOIN friends ON (userid = friendto) WHERE friendfrom = {myid} AND request = 1
Remember you need indexes on the friendto and friendfrom columns for performance
I am doing a pretty normal routine, but having a tough time getting my output correct.
I have two tables: *ads_list* (listings) and *ads_cate* (categories).
I am currently displaying my category list like so:
SELECT id, cateName FROM ads_cate ORDER BY cateName
What I am trying to achieve: count of all items in each category in this format:
Category | Number of Ads
categoryName 56
This is my current code, and have been tweaking but getting no output in my array:
SELECT
ads_cate.id,
ads_cate.cateName, // Category Name
ads_list.id,
ads_list.COUNT(title), // Title of ad
ads_list.Category // Relational Category ID INT(11)
FROM
ads_cate,
ads_list
GROUP BY cateName
ORDER BY cateName
I am calling in all required fields and running a COUNT() on my title field (as these are unique for each ad) and then I am grouping by cateName which also seems correct.
See what this gives you. I think it is what you need.
SELECT
ads_cate.cateName, // Category Name
COUNT(ads_list.id), // Title of ad
FROM
ads_cate
INNER JOIN
ads_list
ON ads_cate.id = ads_list.category
GROUP BY cateName
ORDER BY cateName