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.
Related
I am trying to inner join 3 tables that is from OS TICKET Database.
The code I am using is $qry = "SELECT qbcd_user_email.address, qbcd_user_email.user_id FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.$email.') ORDER BY qbcd_ticket.ticket_id DESC";
Code is returning:
string(287) "SELECT qbcd_user_email.address, qbcd_user_email.user_id FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.patrick.kershner#gmail.com.') ORDER BY qbcd_ticket.ticket_id DESC"
but it is not displaying anything in the while clause:
while ($row = mysqli_fetch_assoc($result)){
echo $row['qbcd_ticket.number]."<br>";}
I am not sure what is going on, or why its not displaying the results.
Can someone check out my code above and verify?
Try to add the number to your selected properties
$qry = "SELECT qbcd_user_email.address, qbcd_user_email.user_id, qbcd_ticket.number FROM qbcd_user_email INNER JOIN qbcd_user ON qbcd_user.id = qbcd_user_email.user_id INNER JOIN qbcd_ticket ON qbcd_ticket.user_id WHERE (qbcd_user_email.address = '.$email.') ORDER BY qbcd_ticket.ticket_id DESC"
the first table is:
qbcd_ticket:
rows:
ticket_id | number | user_id | user_email_id | status_id | dept_id | and more...
5 | 762086| 2 | 0 | 1| 1 |
the next is qbcd_user_email
rows:
id | user_id | flags | address
2 | 2 | 0 | example#demo.com
the last is: qbcd_user
id | org_id | default_email_id | status | name | created | updated
2 | 0 | 2 | 0 | Patrick Kershner | 2017-03-03 10:44:28 | 2017-03-03 10:44:28
The information that I need to display, is all corresponding Tickets associated with the customer where it = the email address.
the only static variable that will not change is $_SESSION['user_email']; which is logged by logging into the members area.
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?
I want to create a SELECT query in mysql.
There are two tables, users and image_info, and need to select following columns.
user table : user_id, username, dob
image_info : image, image_path
When selecting image. I need to get only primary image from image_info table. In image_info table there is a column like:
image_type ENUM('primary', 'gallery') DEFAULT NULL,
This is how I tried it..
$q = "SELECT u.user_id, u.username, u.dob, i.image, i.image_path
FROM users u
INNER JOIN image_info i ON i.user_id = u.user_id
WHERE u.sex = 'Male'
ORDER BY u.date_registered DESC LIMIT 6";
But it doesn't work properly to get my expected output.
UPDATE:
my table outputs..
mysql> select user_id, username, sex from users;
+---------+-------------+--------+
| user_id | username | sex |
+---------+-------------+--------+
| 1 | thara1234 | Male |
| 2 | root234 | Male |
| 3 | kamal123 | Female |
| 4 | Nilantha | Male |
| 5 | Ruwan324324 | Male |
+---------+-------------+--------+
5 rows in set (0.00 sec)
mysql> select user_id, image, image_type from image_info;
+---------+----------------------------+------------+
| user_id | image | image_type |
+---------+----------------------------+------------+
| 2 | 2_root234_1433564588.jpg | primary |
| 1 | 1_thara1234_1433555104.jpg | primary |
| 1 | 1_thara1234_1433556481.jpg | gallery |
| 4 | 4_Nilantha_1433573768.jpg | primary |
+---------+----------------------------+------------+
4 rows in set (0.03 sec)
Thank you.
I think, query would be :-
SELECT User.user_id, User.username, User.dob, Image.image, Image.image_path
FROM
users User LEFT JOIN image_info Image
ON User.user_id = Image.user_id AND Image.image_type = 'PRIMARY'
WHERE User.sex= 'Male'
ORDER BY User.date_registered DESC LIMIT 6
As you said you need the user either he has an image or not you should use left join in your query:
SELECT u.user_id, u.username, u.dob, i.image, i.image_path
FROM users u
LEFT JOIN image_info i ON i.user_id = u.user_id
WHERE u.sex = 'Male' and (i.image_type = 'primary' or i.image_type is null)
ORDER BY u.date_registered DESC LIMIT 6;
See here for more information.
I have a problem. I have 2 database tables.
table 1 people:
+----------+--------------+
| id | name |
+----------+--------------+
| 1 | johanalj |
| 2 | hjgjhggjh |
+----------+--------------+
table 2 images of people:
+----------+--------------+----------------+
| id | url | people_ID |
+----------+--------------+----------------+
| 1 | 3765345.png | 1 |
| 2 | 87e58974.png | 1 |
+----------+--------------+----------------+
Now I want to select person with id 1 from table 1 and all pictures from table 2 that have people_ID 1.
I tried LEFT JOIN in combination with a WHERE but cant get it to work
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE id = '1'";
But I get a no result massage. What am I doing wrong?
There is an error(ambiguous column id). Both tables have id column. You need to add the table alias with id. try with -
$sql = "SELECT * FROM people p LEFT JOIN images i ON i.people_ID = p.id WHERE p.id = '1'";
I have table named 'company' and 'user'. The user table contains the id of company.
Like for example:
A. User table
user_id | name | company_id |status
1 | john | 1 | active
B. Company table
company_id | name | status
1 | ABC | active
How to get the name of the company by their id in single sql query.
such;
$query = "SELECT name as Username, company_id as Company_Name From `user` where status='active'";
That will give the result of:
Username | Company_Name
john | ABC
Any help or ideas on how to do this...
Thanks in advance.
SELECT u.name AS Username, c.name AS Company_Name
FROM User AS u
INNER JOIN Company AS c
ON u.company_id = c.company_id
WHERE u.status = 'active'
AND c.status = 'active'
Feel free to remove either or both of the expressions in the WHERE clause if they don't make sense.