I have three tables:
Table(attribute1, attribute2...);
---------------------------------
Users(iduser, username)
Link(idlink, title, userid)
Comment(idcomment, content, linkid, userid)
How to select:
Link title, with corresponding username and number of comments?
I'm currently doing like this:
Q1-Select links (SELECT * FROM `links`)
Q2-Extract usernames from previous query(Q1) - (SELECT username FROM `user` WHERE iduser=Q1.userid
Q3-Extract number of comments from Q1 by id (SELECT COUNT(*) as comments FROM `comment` WHERE linkid='Q1.idlink')
I believe we can do this in much more optimized way. I got idea how to get Link with corresponding username but I got stuck when I need to count comments.
SELECT iduser, username, Link.title, COUNT(idcomment)
FROM Users
LEFT JOIN Link ON (iduser = userid)
LEFT JOIN Comment ON (linkid = idlink)
GROUP BY iduser, idlink
Note that your Comment table is somewhat badly designed - the 'userid' field is not necessary, and can actually lead to situation where you've got a cross-linked record. e.g. a Comment belonging to user A might could be linked to a Link record belonging to user B.
It is good practice to get into the habit of putting the fields you want into both the SELECT and GROUP BY clauses, that way it won't come as such a shock when you have to use an RDBMS that insists on it.
SELECT
`l`.`idlink`,
`l`.`title`,
`u`.`username`,
COUNT(`c`,`idcomment`) AS `comment_count`
FROM `links` `l`
INNER JOIN `users` `u`
ON `l`.`userid` = `u`.`iduser`
LEFT JOIN `comments` `c`
ON `l`.`idlink` = `c`.`linkid`
GROUP BY
`l`.`idlink`,
`l`.`title`,
`u`.`username`
SELECT
l.idlink
, l.title
, l.userid
, u.iduser
, u.username
, c.idcomment
, c.content
FROM Link AS l
JOIN Users AS u ON u.iduser=l.userid
JOIN Comment AS c ON c.linkid=l.idlink
Related
Some background:
I'm developing kind of social network.
There is DB with next tables:
users(user_id, user_name, password, etc.)
posts(post_id, user_id, post_text, post_data, etc.)
followings(relationship_id, follower_id, following_id, added_date_time)
likes(like_id, user_id, post_id, added_date_time)
comments(comment_id, user_id, post_id, comment_text, added_date_time)
The question is:
How can I implement query which would fetch information about what my followings(people I follow) have did recently (e.g. someone liked/commented something) and order this array of info by date_time?
Can I make it using one query? Or I'll have to make multiple queries and handle all this stuff in PHP by myself?
What is the best approach?
You can normalize the output in a select union like this one
SELECT t.type, t.user_id, u.user_name, t.type_id, t.post_id, t.text, t.added_date_time
FROM (
SELECT 'posts' as type, p.user_id, p.post_id as type_id, p.post_id, post_text as text, p.added_date_time
FROM posts p
JOIN followings f ON (f.following_id = p.user_id)
WHERE f.follower_id = #user
UNION
SELECT 'comments' as type, c.user_id, c.comment_id as type_id, c.post_id, comment_text as text, c.added_date_time
FROM comments c
JOIN followings f ON (f.following.id = c.user_id)
WHERE f.follower_id = #user
UNION
SELECT 'likes' as type, l.user_id, l.like_id as type_id, l.post_id, post_text as text, l.added_date_time
FROM likes l
JOIN followings f ON (f.following.id = c.user_id)
JOIN posts p ON (l.post_id = p.post_id)
WHERE f.follower_id = #user
) as t
JOIN users u ON (t.user_id = u.user_id)
ORDER BY added_date_time DESC;
So you can get all the data in only one query.
I hope it works fine for you.
You have to seperate at least the queries for likes and comments, since the result set is different.
Otherwise your query would look e.g. like:
SELECT comment_id,comment_text,added_date_time FROM comments WHERE user_id IN (SELECT following_id FROM followings WHERE follower_id={{USER_ID_FROM_USER}}) ORDER BY added_date_time
This will get you the comments from people which are followed by {{USER_ID_FROM_USER}} ordered by date.
I have these tables currently:
assignments(assignment_id, module_id, year, semester, title, number, weighting)
module(module_id, code, name, crn, course_title)
usermodule(usermodule_id, user_id, module_id)
users(user_id, title, first_name, last_name)
I was wondering if anyone could give me guidance on how to get the lecturer name, based on the module_id?
So far I've come up with:
SELECT `first_name` FROM `users` WHERE `assignments.module_id` = '$module_id';
I think I may have to do some form of JOIN query...
try this query
SELECT u.first_name FROM users u
LEFT JOIN usermodule um ON um.user_id = u.user_id
WHERE um.module_id = '$module_id';
How about
SELECT `first_name`
FROM `users` u INNER JOIN
`usermodule` um ON u.user_id = um.user_id
WHERE `um.module_id` = '$module_id';
Try rhis:
SELECT `first_name` FROM
`users` INNER JOIN `usermodule` ON `users.user_id`=`usermodule.user_id`
INNER JOIN `assignments` ON `usermodule.module_id` =`assignments.module_id`
WHERE `assignments.module_id` = '$module_id';
Just try below sql
SELECT `first_name` FROM `users` WHERE user_id = (select user_id from usermodule where module_id = '$module_id' limit 1))
I see poor structuring of your database.
You do not necessarily need a table usermodule to create relationships between the other tables.
If there is a relationship between a user and a module.
make use of a foreign key.
You could have a user_id in the module to link a user to a given module, then have 'module_id' in assignment to link a given assignment to a module, that way in the end, the user, module and assignment are related.
It will then be easy to use JOIN or simple select query
I have a user table, e.g.
userId
userName
and I have a message table, e.g.
messageId
messageToId
messageFromId
messageContent
I am trying to make a query to pull a message, but also get the user names from the user table based on the messageToId and messageFromId.
I have done this before with only 1 field between tables, e.g.
SELECT message.*, user.userName
FROM message, user
WHERE user.userId = message.messageToId
AND messageId = (whatever)
But I am having trouble with 2 links.
I want the result as follows:
messageId
messageToId
toUserName
messageFromId
fromUserName
messageContent
Any help would be much appreciated, or if someone had another way of attempting a private message system with PHP/MySQL.
You just have to use joins and different table aliases:
SELECT m.*, u1.userName AS toUserName, u2.username AS fromUserName
FROM message m INNER JOIN user u1 ON m.messageToId = u1.userId
INNER JOIN user u2 ON m.messageFromId = u2.userId
WHERE messageId = "XXX";
You need to use a join from to achieve this:
SELECT `m`.*,
`to`.`userName` AS `to`,
`from`.`userName` AS `from`,
FROM `message` `m`
JOIN `user` `to` ON `m`.`messageToId` = `to`.`userId`
JOIN `user` `from` ON `m`.`messageFromId` = `from`.`userId`
WHERE `m`.`messageId` = 1
So you join against the user table twice to get both users for a particular message. To do this you need to use table aliases as I have done with to and from so that you distinguish between them.
I have also used a field alias to get their usernames separately eg:
`to`.`username` AS `from`
Will this work?
SELECT b.userName AS author, c.userName AS reciever, a.messageId, a.messageContent FROM message a JOIN user b ON a.messageFromId = b.userId JOIN user c ON a.messageToId = c.userId
$query = $this->db->query("SELECT t1.numberofbets, t1.profit, t2.seven_profit, t3.28profit, user.user_id, username, password, email, balance, user.date_added, activation_code, activated FROM user LEFT JOIN (SELECT user_id, SUM(amount_won) AS profit, count(tip_id) AS numberofbets FROM tip GROUP BY user_id) as t1 ON user.user_id = t1.user_id LEFT JOIN (SELECT user_id, SUM(amount_won) AS seven_profit FROM tip WHERE date_settled > '$seven_daystime' GROUP BY user_id) as t2 ON user.user_id = t2.user_id LEFT JOIN (SELECT user_id, SUM(amount_won) AS 28profit FROM tip WHERE date_settled > '$twoeight_daystime' GROUP BY user_id) as t3 ON user.user_id = t3.user_id where activated = 1 GROUP BY user.user_id ORDER BY user.date_added DESC");
return $query->result_array();
The query works fine running it in phpMyAdmin and returns complete results (in image attached). However, printing the array in CodeIgniter, it has no value for one field ,seven_profit, where it is there in the SQL query ran in phpMyAdmin, just the discrepancy in this one field, from sql to php array... I just can’t see why, when printing the array, that one field, which should have value of 26, contains nothing? Any ideas? I changed the field name from starting with a number in attempt to fix it, but no difference.
I know this is complex and looks horrible, any help or just people coming across something similar would be great to know about, thanks.
Sam
edit: the query a bit more formatted:
$query = $this->db->query("
SELECT
t1.numberofbets, t1.profit, t2.seven_profit, t3.28profit,
user.user_id, username, password, email,
balance, user.date_added, activation_code, activated
FROM
user
LEFT JOIN
(
SELECT
user_id,
SUM(amount_won) AS profit,
count(tip_id) AS numberofbets
FROM
tip
GROUP BY
user_id
) as t1
ON
user.user_id = t1.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(amount_won) AS seven_profit
FROM
tip
WHERE
date_settled > '$seven_daystime'
GROUP BY
user_id
) as t2
ON
user.user_id = t2.user_id
LEFT JOIN
(
SELECT
user_id,
SUM(amount_won) AS 28profit
FROM
tip
WHERE
date_settled > '$twoeight_daystime'
GROUP BY
user_id
) as t3
ON
user.user_id = t3.user_id
WHERE
activated = 1
GROUP BY
user.user_id
ORDER BY
user.date_added DESC
");
Ok the problem was the SQL query includes PHP variables, these variables must just be treated as 0 or something without giving errors, so it displays a value in the seven_profit column.
When the query is ran in CodeIgniter it uses the correct value for $seven_daystime variable, and therefore the result is different when the data is put into the array.
/blames tiredness
Let's say I have two tables, users and notes. Let's say the schemas look like this:
users
id, name, field2, field3
notes
id, user_id, subject, heading, body, timestamp
What I want to do is select every user, and the LATEST (just 1) note posted by each user based on the timestamp to show in an overview report.
How would I go about doing this? Please note that the DB is MySQL.
Thanks!
select users.name, notes.subject, notes.heading, notes.body
from users, notes
where users.id = notes.user_id
and notes.timestamp = (select max(timestamp) from notes where user_id = users.id)
select u.id, u.name, n.id as note_id, n.subject, n.heading, n.body, n.timestamp
from users u
left outer join (
select user_id, max(timestamp) as timestamp
from notes
group by user_id
) nm
left outer join notes n on nm.user_id = n.user_id and nm.timestamp = n.timestamp
Note that this could potentially return duplicates if the user has two notes with the exact same timestamp. I have assumed this is not the case.
SELECT *
FROM `users`
LEFT JOIN `notes`
ON `user_id` = `users`.`id`
WHERE `notes`.`timestamp` = (
SELECT MAX(`timestamp`)
FROM `notes` AS `notes_1`
WHERE `notes_1`.`user_id` = `notes`.`user_id`
)