I've a following query to select data from a table (chat) in a chat system:
SELECT * FROM (SELECT * FROM chat WHERE id_chat = '$chat_id' ORDER BY id DESC LIMIT 10) S
WHERE id_chat = '$chat_id'
ORDER BY id ASC
LIMIT 10
In this table (chat), there is a user column where it is the user id that sent the message.
I would, from the id_from of the user who sent the message, get back data that user (users table).
Table chat:
id | id_chat | id_from | id_to | message | date
1 | 23 | 1 | 2 | hello! | 01-12-2016
Table users:
id | name | photo
1 | John | pic.png
2 | Nick | hey.jpg
What better way to do it using the above query? LEFT JOIN? INNER JOIN? And, how do?
Related
I have a table that stores user_avatar filepath :
id | userid | file | last_update |
1 | 23 | myphoto.png | x_timestamp |
2 | 23 | myphoto2.png | x_timestamp |
3 | 25 | myavatar.png | x_timestamp |
I have a table that returns user_score :
id | gamerid | score | last_update |
1 | 23 | 44 | x_timestamp |
2 | 25 | 99 | x_timestamp |
This is the query I use to display user scores with their avatar
SELECT * FROM user_score us LEFT JOIN user_avatar ua ON us.gamerid=ua.userid
Above query will return 2 duplicate rows for user 23 because he as downloaded 2 different photos in the user_avatar table. Right now it outputs like this:
GAMER SCORES RESULTS
myphoto.png | 44
myphoto2.png | 44
myavatar.png | 99
In reality, only one row should be returned for each user and the last avatar downloaded should be used as primary avatar. How can I fix this?
You can try below -
SELECT * FROM user_score us LEFT JOIN user_avatar ua ON us.gamerid=ua.userid
and ua.last_update =
(select max(last_update) from user_avatar ua1 where ua.userid=ua1.userid)
You should select the value for the most recent avatar .. probably related ton the max id for user_id
SELECT ua.*, us.*
FROM user_score us
INNER JOIN (
select user_id, max(id) max_id
from user_avatar
) t on t.user_id = us.user_id
LEFT JOIN user_avatar ua
ON us.gamerid=ua.u serid
AND t.max_id = ua.id
In reality, the last avatar downloa
why does this SQL Code not run?
user_chats
id | user_id | to_user_id | ad_id | timestamp
----------------+---------+------------+---------+-----------
1 | 1 | 6 | 13 | 1513516133
user_messages
id | chat_id | text | user_id | timestamp
----------------+---------+------------+---------+-----------
1 | 1 | Hello | 1 | 1513516133
2 | 1 | Hi! | 6 | 1513516754
I want to get the Chats and order them by user_messages.timestamp.
My SQL Code is:
SELECT user_chats.id,
user_chats.timestamp,
ad_id,
title,
user_chats.user_id
FROM user_chats
INNER JOIN ads
ON ads.id = ad_id
WHERE user_chats.user_id = "1"
OR user_chats.to_user_id = "1"
ORDER BY (SELECT id
FROM user_messages
WHERE chat_id = user_chats.id
ORDER BY user_messages.id DESC)
The issue is that you've used a subquery in your Order By clause: as this returns multiple results for each record in the main query it cannot be used to order the results of the main query.
I think you're trying to order the results by the latest message in each chat, but simply joining the user_messages table will mean you'll get duplicates (each chat being returned once per message). You can get around this by joining to an inline view:
SELECT DISTINCT user_chats.id,
user_chats.timestamp,
ad_id,
title,
user_chats.user_id
FROM user_chats
INNER JOIN ads
ON ads.id = ad_id
LEFT JOIN
--in line view aliased 'UM' returns one row per chat_id in user_messages, with the last timestamp for that ID
(SELECT max(timestamp) LastMessage,
chat_id
FROM user_messages
GROUP BY chat_id) um
ON um.chat_id = user_chats.id
WHERE user_chats.user_id = 1
OR user_chats.to_user_id = 1
ORDER BY um.LastMessage desc
Hello I am trying to get number of unread replies from administrator. Those are support tickets. There are three tables like below.
table: supportticket
id | date | username | email | status | subject | message | adminreply |
admindate | read
table: ticketreplies
id | tid | userid | name | email | date_added | message | admins
table: tickets
id | userid | trackid | name | email | date_added | title | message |
status | urgency | lastreply
table: ticket_read
id | idt | lu
I tried this:
$query = "SELECT COUNT(*) FROM first table WHERE to_user_id
=".$_SESSION['user_id']." AND `read` = 0";
But without success. Hope for more ideas. I am new at this.
*when ticket is created by user this table gets update:
tickets
id | userid | trackid | name | email | date_added
| title | message | status | urgency | lastreply
456 2 123-456 user user#user.com 3/23/2017
15:52 test test open medium
3/24/2017 15:52
* when admin replies
ticketreplies:
id | tid | userid | name | email | date_added
| message | admins 0 456 0
3/23/2017 15:52 replied to test 1
ticket_read
id | idt | lu
0 456 1
p.s. when admin replies tickets table gets status Answered and lastreply get new date time
check image: prntscr.com/eo0pj2
I am sure it is not to_user_id but id in first_table so change like this
$query = "SELECT COUNT(*) FROM `first_table` WHERE `id`
='".$_SESSION['user_id']."' AND `read` = 0";
Assuming you have not provided exact table name that why it is first table not as first_name
Try like this
$query = "SELECT COUNT(*) FROM first table
WHERE first_table.id =".$_SESSION['user_id']." AND `read` = 0";
Since not much information is given in the question (no logical connection between tables is provided in the question), then in case id is just a reference number of the ticket you could try this.
$query = "SELECT COUNT(*) FROM supportticket
INNER JOIN tickets
ON supportticket.id=tickets.id
WHERE tickets.userid =".$_SESSION['user_id']." AND supportticket.read = 0";
OR this
$query = "SELECT COUNT(*) FROM supportticket
INNER JOIN ticketreplies
ON supportticket.id=ticketreplies.id
WHERE ticketreplies.userid =".$_SESSION['user_id']." AND supportticket.read = 0";
As I understand your question and table structure, You should use INNER JOIN to this. And make some changes to your tables.
Example:
SELECT COUNT(*) as admin_replied FROM first_table
INNER JOIN second_table ON first_table.trackid = second_table.trackid
WHERE user_id=".$_SESSION['user_id']." AND `read` = 0";
The first_table should contain trackid as your second_table does. This ticket id will be your key to get all the unread reply of admin in a particular user.
But this will be change according on how you store the message and replies to your tables.
I am trying to built the messaging system and I want to select distinct value from the table and sort by date and also return the read and unread status. My table structure is as id, from_user_id, to_user_id, message, datetime, read . Suppose dummy data are as follows:
id | from_user_id | to_user_id | message | datetime | read
1 | 20 | 50 | hi | 2016-3-13 06:05:30 | 1
2 | 20 | 50 | hey | 2016-3-13 06:15:30 | 0
3 | 30 | 50 | hi | 2016-3-14 06:05:30 | 0
Now I want to select distinct from_user_id and sort by date so that I can display the name of the user who is chating recently on top of list and also return the read status 1, if any of the rows with from_user_id has read status 0, then I want to return read as 0 so that I can differentiate whose user message has unread message.
Suppose, from_user_id has read status as 0 in one rows then I want to return read status as 0 when returning distinct value of from_user_id. How can I do it. I tried as follows but won't work in my case. It returns distinct value but not sorted by date and also read status in not returning what I expect.
select distinct(`from_user_id`),register.name FROM message INNER JOIN register ON register.id = message.from_user_id where message.to_user_id = $user_id GROUP BY message.id ORDER BY MAX(datetime) ASC
Try the following query:
SELECT
message.from_user_id,
register.name,
message.read
FROM message
INNER JOIN
(
SELECT
from_user_id,
MAX(datetime) max_datetime
FROM message
WHERE to_user_id = 50
GROUP BY from_user_id ) t
ON t.from_user_id = message.from_user_id AND t.max_datetime = message.datetime
INNER JOIN register ON register.id = message.from_user_id
WHERE message.to_user_id = 50
ORDER BY message.datetime ASC
UPDATED SQL FIDDLE
Sample Input:
id | from_user_id | to_user_id | message | datetime | read
1 | 20 | 50 | hi | 2016-3-13 06:05:30 | 1
2 | 20 | 50 | hey | 2016-3-13 06:15:30 | 0
3 | 30 | 50 | hi | 2016-3-14 06:05:30 | 0
Output:
from_user_id name read
20 User20 0
30 User30 0
Note: This query might give you multiple row for the same from_user_id if there exists multiple entries in your message table having the same datetime and same to_user_id.
checkout this query :
SELECT DISTINCT(form_user_id),MIN(read) FROM table_name ORDER BY datetime ASC
In my page I have a tab named Recent activity, In which I have to display the two different types of actions
Recently added choices
Recently voted choices
The table stucture for je_addchoice
je_addpoll table
poll_id | user_id | poll_name | category_id | start_date | end_date
1 | 20 |Naturalflrs| 18 | 2012-12-03 | 2095-12-25
je_addchoice table
choice_id | poll_id | choice_creator_id | choice_name | choice_image | description | ctime
1 | 1 | 20 | Greenish | forest.jpg | forest |135453
je_user_vote table
vote_id | user_id | poll_id | choice_id | datetime_voted | user_type
12 | 31 | 1 | 1 |12-12-2606:23:17| normal
Already I have the result page displays as shown below
The above result is displayed using the query
$result=mysql_query("SELECT * FROM je_addchoice, je_addpoll where je_addpoll.start_date <= '$check_date' AND je_addpoll.end_date >='$check_date' AND je_addpoll.poll_id=je_addchoice.poll_id order by je_addchoice.choicecreationtime desc");
The two tables
1) je_addpoll (Main table for polls)
2) je_addchoice (adding choices for the polls)
But what I want to do here is If any user votes for the poll, It will store into
je_user_vote table as shown above.
I want to display the recently voted choices in the same tab
Try this
use desc order for datetime_voted just before je_addchoice.choicecreationtime desc
like this.
$result=mysql_query("SELECT * FROM (SELECT P.poll_id,P.user_id,P.poll_name,P.category_id,P.start_date,P.end_date,C.choice_id,C.choice_creator_id,C.choice_name,C.choice_image,C.description,C.ctime FROM je_addchoice C, je_addpoll P where P.start_date <= '$check_date' AND P.end_date >='$check_date' AND P.poll_id=C.poll_id order by C.choicecreationtime desc) N,je_user_vote U WHERE U.poll_id=N.poll_id order by U.datetime_voted desc,N.choicecreationtime desc");
OR
$result=mysql_query("SELECT N.poll_id,N.user_id,N.poll_name,N.category_id,N.start_date,N.end_date,N.choice_id,N.choice_creator_id,N.choice_name,N.choice_image,N.description,N.ctime FROM (SELECT P.poll_id,P.user_id,P.poll_name,P.category_id,P.start_date,P.end_date,C.choice_id,C.choice_creator_id,C.choice_name,C.choice_image,C.description,C.ctime FROM je_addchoice C, je_addpoll P where P.start_date <= '$check_date' AND P.end_date >='$check_date' AND P.poll_id=C.poll_id order by C.choicecreationtime desc) N,je_user_vote U WHERE U.poll_id=N.poll_id order by U.datetime_voted desc,N.choicecreationtime desc");