The tables are as below:
Users Table (Table Name: users) :
[users table]
Friends Table (Table Name: friends) :
[friends table]
My Sql Code:
SELECT
friend_id,
friend_adding_id,
friend_added_id,
user_id,
user_name
FROM friends
LEFT JOIN users ON friends.friend_added_id = users.user_id
WHERE
friend_adding_id = 1
OR friend_added_id = 1
AND friend_confirm = 1
ORDER BY friends.friend_id DESC
LIMIT 1, 10
Leaving the column below:
[Leaving column]
How can I fix this missing query? Please help me.
first you are missing a row not a column or not a query, second its because of and /or priorities. And has higher priority than OR , for you logic to work properly, you have to give Or higher priority by using parenthesis:
SELECT
friend_id,
friend_adding_id,
friend_added_id,
user_id,
user_name
FROM friends
LEFT JOIN users ON friends.friend_added_id = users.user_id
WHERE
(friend_adding_id = 1 OR friend_added_id = 1) -- < like this
AND friend_confirm = 1
ORDER BY friends.friend_id DESC
LIMIT 1, 10
Related
I'm working on a query that doesn't behave as expected, and would appreciate any help on pointing me in the right direction.
TABLES
I have three central tables consists of the following.
table_categories
- id (int)
- name (varchar)
table_events
- id (int)
- name (varchar)
- created (date time)
table_taxonomy
- cat_id (id from table_categories)
- event_id (id from table_events)
- type (varchar in this example always 'category')
GOAL
Count events created after a certain date in each category. The result should be something like:
COUNT NAME ID
3 Rock 1
2 Punk 3
QUERY
This is the query that I've come up with. The problem is that the result doesn't care about the created date, and grabs all events regardles of when they where created.
SELECT COUNT(*) AS count,
table_categories.name,
table_categories.id
FROM table_taxonomy
INNER JOIN table_categories
ON table_categories.id = table_taxonomy.cat_id
LEFT JOIN table_events
ON table_events.id = table_taxonomy.events_id
WHERE table_taxonomy.type = 'category'
AND table_taxonomy.cat_id IN(2,3)
AND table_events.created > '2012-10-07 05:30:00'
GROUP BY (table_categories.name)
try this one, just small change while comparing date :
SELECT COUNT(*) AS count,
table_categories.name,
table_categories.id
FROM table_taxonomy
INNER JOIN table_categories
ON table_categories.id = table_taxonomy.cat_id
LEFT JOIN table_events
ON table_events.id = table_taxonomy.events_id
WHERE table_taxonomy.type = 'category'
AND table_taxonomy.cat_id IN(2,3)
AND Date(table_events.created) > '2012-10-07 05:30:00'
GROUP BY (table_categories.name)
Try this one,
SELECT c.ID, c.Name, COUNT(*)
FROM table_categories a
INNER JOIN table_taxonomy b
ON a.id = b.cat_id
INNER JOIN table_events c
ON b.event_ID = c.ID
WHERE c.created > 'dateHere'
-- AND ..... -- other conditions here
GROUP BY c.ID, c.Name
I have two sql tables one which has users and the other which has messages.
Right now my query is :
SELECT users.memberid,users.username,users.profileimage,users.gender,message.messagebody, message.fromid,message.toid,message.messageid
FROM message,users
WHERE message.fromid = users.memberid AND message.toid = '$id' AND recieverdeleted='0'
ORDER BY datetime DESC LIMIT 55
Right now what is being returned is all the information for everymessage regardless of formid(sender's id)
The thing is , I want to only display the most recent message for every from id ..Kind of like how Facebook only shows you the most recent message that friend x has sent you, and not all the messages. I will work on showing all messages after the user has clicked on his friend's most recent message.
Thanks
Well, you tried. That's good.
SELECT u.memberid
, u.username
, u.profileimage
, u.gender
, m.messagebody
, m.fromid
, m.toid
, m.messageid
FROM users u
JOIN message m
ON m.fromid = u.memberid
JOIN (SELECT fromid,toid,MAX(datetime) max_datetime FROM message GROUP BY fromid,toid) n
ON n.fromid = m.fromid
AND n.toid = m.toid
AND n.max_datetime = m.datetime
WHERE message.toid = $id
AND recieverdeleted = 0
ORDER
BY datetime DESC LIMIT 55;
At first join the two tables than apply order by because at this moment the command is not sure for which table you want to apply the ordering.
Or better you create an intermediate table from the selection of two tables and then apply the ordering.
Something like:
SELECT username,messagebody,fromid FROM(
SELECT users.memberid,users.username,users.profileimage,users.gender,message.messagebody, message.fromid,message.toid,message.messageid,message.datetime
FROM message,users
WHERE message.fromid = users.memberid AND message.toid = '$id' AND recieverdeleted='0' )INTERMEDIATE_TABLE ORDER BY datetime DESC
I might be wrong in syntax as I have done sql codes a long ago but you should try something very similar like this.
Try this its working
SELECT m1.*
FROM table_name m1
INNER JOIN (SELECT MAX(senddate) AS senddate,
IF(member_id2 = 3, member_id1, member_id2 ) AS user
FROM table_name
WHERE (member_id1 = 3 AND delete1=0) OR
(member_id2 = 3 AND delete2=0)
GROUP BY user) m2
ON m1.senddate = m2.senddate AND
(m1.member_id1 = m2.user OR m1.member_id2 = m2.user)
WHERE (member_id1 = 3 AND delete1=0) OR
(member_id2 = 3 AND delete2=0)
ORDER BY m1.senddate DESC
Try this::
SELECT users.memberid,users.username,users.profileimage,users.gender,message.messagebody, message.fromid,message.toid,message.messageid
FROM message inner join users on (message.fromid = users.memberid)
where message.toid = '$id' AND recieverdeleted='0'
ORDER BY message_datetime DESC limit 1
I use the following MySQL to return a list of posts and their corresponding comments.
SELECT *
FROM forum_qa
JOIN user_profiles
ON user_id = forum_qa_author_id
LEFT JOIN (SELECT forum_cm_id,
forum_cm_author_id,
forum_qa_id_fk,
forum_cm_text,
FROM forum_cm
JOIN user_profiles
ON user_id = forum_cm_author_id) AS c
ON forum_qa_id = c.forum_qa_id_fk
WHERE forum_qa_parent_id = $forum_qa_id
If I run
$data['num_answers'] = $query->num_rows();
This allows me to get the number of returned rows and pass the array to my controller and view.
But this is returning all rows (posts + comments). So if 1 post has 10 comments, it returns 10.
How could I have this query count only the number of posts (ie, returning 1) not including the subquery?
Each post has a unique id saved in forum_qa.forum_qa_id
Each comment has a unique id saved in forum_cm.forum_cm_id.
Thanks for helping -- will post more code if needed.
Not the fastest, but you are not restricted in using GROUP BY:
SELECT *,
(SELECT COUNT(*) FROM forum_qa WHERE forum_qa_parent_id = $forum_qa_id) Cnt
FROM forum_qa
JOIN user_profiles
ON user_id = forum_qa_author_id
LEFT JOIN (SELECT forum_cm_id,
forum_cm_author_id,
forum_qa_id_fk,
forum_cm_text,
FROM forum_cm
JOIN user_profiles
ON user_id = forum_cm_author_id) AS c
ON forum_qa_id = c.forum_qa_id_fk
WHERE forum_qa_parent_id = $forum_qa_id
You can run another query or add one more column (with an independent subquery) in the result set:
SELECT *
, ( SELECT COUNT(*)
FROM forum_qa
WHERE forum_qa_parent_id = $forum_qa_id
) AS cntPosts
FROM forum_qa
JOIN user_profiles
ON user_id = forum_qa_author_id
LEFT JOIN (SELECT forum_cm_id,
forum_cm_author_id,
forum_qa_id_fk,
forum_cm_text,
FROM forum_cm
JOIN user_profiles
ON user_id = forum_cm_author_id) AS c
ON forum_qa_id = c.forum_qa_id_fk
WHERE forum_qa_parent_id = $forum_qa_id
COUNT(DISTINCT forum_qa.forum_qa_id)
COUNT(DISTINCT col_name) counts the distinct post ids. This should equal the number of posts.
Table A is named "users" and Table B is named "comments". I want to select fields "user_id" and "user_name" from Table A and "date", "comment", "user_id" fields in Table B.
In a nutshell I am trying to get all the users and their comments with 1 query. Can the comments be put in its own array as there will be multiple comments like below or will it just be 1 comment and would have to run 2 queries?
Basic Example
user_id = 1
username = foo
comments[0]['date'] = 1234567890
comments[0]['comment'] = "Hello World!"
comments[1]['date'] = 1234567890
comments[1]['comment'] = "MySQL n00b"
Any basic examples would be great so I can get my head round it.
This would be a general query, to select all the comments of user_id = 1.
SELECT u.user_id, u.user_name, c.`date`, c.`comment`
FROM comments c
INNER JOIN users u ON c.user_id = u.user_id
WHERE c.user_id = 1
Assuming the user_id is 1, here is the code if you want an associative array with all user's comments. There is only one query, using JOIN statement.
$res = mysql_query("SELECT users.user_id as uid, users.user_name as username, comments.date, comments.comment, comments.user_id as cid FROM `users` LEFT JOIN comments on uid=cid WHERE cid = 1");
$i = 0;
while($arr = mysql_fetch_assoc($res))
{
$comments[$i]['date'] = $arr['date'];
$comments[$i]['comment'] = $arr['comment'];
$i++;
}
//var_dump($comments);
We call this JOIN. Better to take some sql/mysql tutorial or you get lost.
I have a query like below...
SELECT
contents.id, contents.title, contents.createdBy,
(SELECT userGroup FROM suser_profile WHERE userId =
(SELECT users.id
FROM
users
WHERE
login = contents.createdBy)
) as userGroupID
FROM
contents
WHERE
contents.id > 0
AND contents.contentType = 'News'
**AND userGroupID = 3**
LIMIT 0, 10
When I try to assign the userGroupID inside WHERE clause the SQL fires an error saying SQL Error(1054):Unknown column "userGroupID" in "where clause"
meantime, if I make little changes like below,,
SELECT
contents.id, contents.title, contents.createdBy
FROM
smart_cms_contents
WHERE
contents.id > 0
AND contents.contentType = 'News'
**AND (SELECT userGroup FROM user_profile WHERE userId =
(SELECT users.id
FROM
users
WHERE
users.login = contents.createdBy)
) = 3**
LIMIT 0, 10
then the query works fine.
I have to use multiple userGroupID checking so that, 2nd style will make the query big, I have to have an style like first one, any help appreciated.
*NOTE : Table names are not original name what I am using in my project. You may ignore it if there are mistakes in table name. My main concern is on using the values assign to a variable by AS inside the WHERE clause.
Ignore the STARS in query*
use HAVING. example:
WHERE
contents.id > 0
AND
contents.contentType = 'News'
HAVING
userGroupID = 3
LIMIT 0, 10
If I'm understanding your initial query properly, then I believe what you want to do is a join:
SELECT DISTINCT
contents.id, contents.title, contents.createdBy
FROM
contents INNER JOIN users
ON contents.createdBy = users.login
INNER JOIN user_profile
ON user_profile.userId = users.id
WHERE
contents.id > 0
AND contents.contentType = 'News'
AND user_profile.userGroupID = 3
LIMIT 0, 10
Totally not the answer to the question you asked, but...
SELECT DISTINCT c.id, c.title, c.createdBy, s.userGroup AS userGroupID
FROM contents AS c
INNER JOIN users AS u
ON c.createdBy = u.login
INNER JOIN suser_profile AS s
ON s.userId = u.id
WHERE c.id > 0
AND c.contentType = 'News'
AND s.userGroup = 3