I am developing a social website.I have an option called alerts which shows pending friend requests and unread messages.My query is following:
SELECT
sk_friends.frndship_from_user_id,
sk_messages.msg_from_user_id,
sk_messages.msg_text,
sk_messages.msg_date
FROM
sk_friends INNER JOIN sk_messages
WHERE
sk_messages.msg_to_user_id = '$user_id'
AND sk_friends.frndship_to_user_id ='$user_id'
AND sk_friends.frndship_status = 'pending'
AND sk_messages.msg_status='unread'
ORDER BY
sk_friends.fndship_date ASC,
sk_messages.msg_date ASC;
sk_friends and ak_messages are tables.
msg_from_user_id is the id of sender
frndship_from_user_id is the id of the user who sends the request
$user_id is the id of the login user
Each row data is appearing twice. I dont know why does it happen.
Your inner join does not have an ON clause. Try adding one, that should remove double results.
Try this :
SELECT
sf.frndship_from_user_id,
sm.msg_from_user_id,
sm.msg_text,sm.msg_date
FROM
sk_friends sf ,
sk_messages sm
WHERE
sm.msg_to_user_id = sf.frndship_to_user_id AND
sm.msg_to_user_id = '$user_id' AND
sf.frndship_status = 'pending' AND
sm.msg_status='unread'
ORDER BY
sf.fndship_date ASC,sm.msg_date ASC;
Related
My tables
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY id DESC
LIMIT 1";
This is my SQL query, my task is to show the last records from 3 tables, but the table is blank, I don't know why,thanks in advance people :)
I guess the problem is coming from the ORDER BY id DESC .
Indeed, you have no column so called id.
You should probably remove this clause, in order to make your code work.
If you want to take the last records anyway, you can put an ORDER BY address_id DESC which will do the job !
The code directly edited :
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY adress_id DESC
LIMIT 1";
This may work:
SELECT a.address_id, u.user_id, n.note_id
FROM addresses a
LEFT JOIN users_addresses ua ON ua.ua_address_id = a.address_id
LEFT JOIN users u ON u.user_id = ua.ua_user_id
LEFT JOIN notes n ON n.note_user_id = u.user_id
ORDER BY a.address_id DESC
LIMIT 1
Here is the query to get all data from all the tables, not sure what do you mean last records from 3 tables, I can see four tables there:
SELECT *
FROM `addresses`
LEFT JOIN `users_addresses` ON `users_addresses`.`ua_address_id` = `addresses`.`address_id`
LEFT JOIN `users` ON `users`.`user_id` = `users_addresses`.`ua_user_id`
LEFT JOIN `notes` ON `notes`.`note_user_id` = `users`.`user_id`;
I got two tables, customer_ledger and users.
I'm using PHP for my simple log-in program wherein the users input their username and password. Once the account is valid, it will show them their billing history. So I joined the two tables to link the username with the contract number in the other table.
My problem now is, how to display their (5) latest billings based on the field (LDGR_PER_COV_TO)? I created a MySQL query but it gave me a different result.
This is my query:
SELECT
customer_ledger.LDGR_YEAR,
customer_ledger.LDGR_MONTH,
customer_ledger.LDGR_PER_COV_FROM,
customer_ledger.LDGR_PREV_RDNG,
customer_ledger.LDGR_PER_COV_TO,
customer_ledger.LDGR_PRES_RDNG,
customer_ledger.LDGR_KWH_USED,
customer_ledger.LDGR_BILL_AMOUNT
FROM
customer_ledger
INNER JOIN users
ON customer_ledger.LDGR_CONTRACT_NO=users.LDGR_CONTRACT_NO
WHERE users.Username = 'kim'
ORDER BY customer_ledger.LDGR_PER_COV_TO ASC
LIMIT 5
this result will give me rows starting from the top most record. I would like it to display from the bottom-going up (latest record - top record).
Can anyone help?
You should use alises for better readibility,
Then use DESC in place of ASC
SELECT cl.LDGR_YEAR, cl.LDGR_MONTH,
cl.LDGR_PER_COV_FROM, cl.LDGR_PREV_RDNG,
cl.LDGR_PER_COV_TO, cl.LDGR_PRES_RDNG,
cl.LDGR_KWH_USED, cl.LDGR_BILL_AMOUNT
FROM customer_ledger AS cl INNER JOIN users AS u
ON cl.LDGR_CONTRACT_NO=u.LDGR_CONTRACT_NO
WHERE u.Username = 'kim'
ORDER BY cl.LDGR_PER_COV_TO DESC LIMIT 5
New Query ::
SELECT * FROM
(
SELECT cl.LDGR_YEAR, cl.LDGR_MONTH,
cl.LDGR_PER_COV_FROM, cl.LDGR_PREV_RDNG,
cl.LDGR_PER_COV_TO, cl.LDGR_PRES_RDNG,
cl.LDGR_KWH_USED, cl.LDGR_BILL_AMOUNT
FROM customer_ledger AS cl INNER JOIN users AS u
ON cl.LDGR_CONTRACT_NO=u.LDGR_CONTRACT_NO
WHERE u.Username = 'kim'
ORDER BY cl.LDGR_PER_COV_TO DESC LIMIT 5
) AS a
ORDER BY a.LDGR_PER_COV_TO
I need a query in sql which can get me the details of the last message of all the friends of the user .The fields in my sql database are :
message_id (primary key)
sender (username of the sender)
message_content (text of the message)
user_id (user id of the app user)
friend_id (user id of the app user's friends)
message_time (time of the message receive on server)
receiver (user id of the receiver)
Edit : So far I think my closest try was
Select * from user_chat_messages where message_id on (Select distinct message_id from user_chat_messages order by message_time limit 1,0);
Rest all other queries that I tried were a total failure :(
This is a very common mysql problem where you need to join a table on itself to get a result set with the min / max of each unique user.
Try something like this:
SELECT t1.* FROM table t1
JOIN (
SELECT MAX(message_time) AS 'time', friend_id, user_id
FROM table GROUP BY friend_id, user_id
) t2 ON (t1.message_time = t2.time AND t1.friend_id = t2.friend_id AND t1.user_id = t2.user_id)
Essentially your subquery finds the latest time grouped by friend_id and then joins it back to the main table so that it only pulls the record with the latest time from the first table. From there you can add a WHERE statement to only show the latest messages from a specific user - adding the condition in the subquery will actually improve the performance
SELECT t1.* FROM table t1
JOIN (
SELECT MAX(message_time) AS 'time', friend_id, user_id
FROM table
WHERE user_id=? GROUP BY friend_id
) t2 ON (t1.message_time = t2.time AND t1.friend_id = t2.friend_id AND t1.user_id = t2.user_id)
i am creating groups for my website. i have following tables
user table which contain all user details
cc_group_member table for users group details in this i have user id, group id
communication_center_groups table for groups .
i want to search user for add in a group. but when i search then it show duplicate data. problem is this.. which user is alreay a member of that group it is also show in search result.
i have 4 invitation status in group table which reffer
0- send request
1-accpet
2-reject
3-cancel.
i want if user reject or cancel request thay also show in result but only 1 time it show 2 times in my result. please help..
thanx.
SELECT u.*,cc.invitation
FROM user u
left join (select distinct(user_id),invitation
from cc_group_member
where group_id='$groupId'
and (invitation = '3' or invitation = '2') ) as cc ON u.id = cc.user_id
where (u.username like '".$condition."%' or u.first_name like '".$condition."%' or last_name like '".$condition."%')
and u.type !='1' and u.type = '$user_type'
ORDER BY u.username
SELECT u.*,cc.invitation
FROM user u left join (select distinct(user_id),invitation
from cc_group_member
where group_id='10'
and (invitation = '3' or invitation = '2') ) as cc ON u.id = cc.user_id
where (u.username like '1%' or u.first_name like '1%' or last_name like '1%')
and u.type !='1' and u.type = '5'
ORDER BY u.username
I could not understand your question.but if your are having duplicate result you can make use of select distinct and for your second part you can add another field in database which tells whether this user is member or not
You can use UNION
http://www.w3schools.com/sql/sql_union.asp
simply use union with your different condition and get your all records
SELECT POSTS.*
FROM POSTS
LEFT JOIN SUBSCRIBERS
ON POSTS.AUTHORID = SUBSCRIBERS.PROFILEID
WHERE POSTS.AUTHORID = ?
OR SUBSCRIBERS.SUBSCRIBERID = ?
ORDER BY POSTID DESC
LIMIT ?
The above is the query I'm using and this will select posts from the profiles a user is subscribed to. Now this works fine, but now I want to grab another column from another table.
In my third table USERS, I have an "AVATARID" which I want to access.
In my fetch loop for this query, it echos out the username of the post author, and the post body. What I want it to do is also echo out the avatarID of the user who wrote the post. I tried adding in another query inside my while loop, but I find that sloppy, and it also doesn't work :S
So, simple question: How do I access the AvatarID from the table USERS with the USERID of the AUTHORID from the post?
Something like...
SELECT POSTS.*, USERS.AVATARID
FROM POSTS
LEFT JOIN SUBSCRIBERS ON POSTS.AUTHORID = SUBSCRIBERS.PROFILEID
JOIN USERS ON POSTS.AUTHORID = USERS.USERID
WHERE POSTS.AUTHORID = ?
OR SUBSCRIBERS.SUBSCRIBERID = ?
ORDER BY POSTID DESC
LIMIT ?