How to count unanswered threads mysql/eloquent - php

Hi I have table threads and questions with relation one-to-many.
every message has foreign key thread_id.
I want to check is last message in the thread is from user and if yes then count it, so it will count all unanswered threads by admin.
+----+-------------+---------+----------+------+
| id | thread_id | user_id | admin_id | text |
| 1 | 1 | 1 | NULL | test |
| 2 | 1 | 1 | NULL | test |
| 3 | 1 | 1 | NULL | test |
| 4 | 1 | 1 | NULL | test |
+----+-------------+---------+----------+------+
The result from this dataset should be 1, because all mesages belongs to the same thread and there is no admin answer (last row has admin_id = NULL).
I tried this:
ThreadMessage::where('admin_id', '=', null)->groupBy('thread_id')->orderBy('id', 'DESC')->limit(1)->count();
Sadly this just counts all the messages where admin_id is null, not just the last ones in thread.
The answer can be in mysql OR laravel eloquent. Thank you very much.

To check for null you need to use IS NULL not = NULL operator. Use whereNull() method
ThreadMessage::whereNull('admin_id')->groupBy('thread_id')->count();
EDIT
To check if the last id is reponded to by admin, you can do it in two ways
with code
$thread = ThreadMessage::orderBy('thread_id', 'desc')->groupBy('thread_id')->first();
$answeredByAdmin = $thread->admin_id != null;
With query (assuming the table name is thread_messages)
ThreadMessage:::whereNull('admin_id')
->groupBy('thread_id')
->whereIn('id', \DB::raw('(SELECT MAX(id) FROM thread_messages GROUP BY question_id)'))
->count();

Related

Laravel get latest records with group by with multiple column

Hello I'm new in Laravel I'm stuck in a complex query with Laravel eloquent
Here is my messages table structure
---|-----------|-------------|--------
ID | sender_id | receiver_id | message
---|-----------|-------------|--------
1 | 1 | 2 | Hello
2 | 1 | 2 | Hey hello reply
3 | 2 | 1 | Good
4 | 3 | 2 | Hey GOOD Morning
5 | 3 | 2 | good night
I want to retrieve recent chat from messages table either user is sender or receiver I want to retrieve with a groupBy using multiple columns and with latest only one message
Like for User 2 (Either sender or receiver)
-----------|-------------|---------------
sender_id | receiver_id | message
-----------|-------------|---------------
2 | 1 | Good
3 | 2 | good night
Here is my eloquent query
$user = auth()->user(); //User_id 2 based on logged in user either sender or reciever
Message::where('user_id', $user->id)
->orWhere('receiver_id', $user->id)
->select('user_id','receiver_id')
->groupBy('user_id','receiver_id')
->get();
Result
all: [
App\Models\Message {
user_id: 1,
receiver_id: 2,
}, // don't want this record
App\Models\Message {
user_id: 2,
receiver_id: 1,
},
App\Models\Message {
user_id: 3,
receiver_id: 2,
},
],
If I try to use latest() method then it's showing error timestamp is there in a table
Illuminate/Database/QueryException with message 'SQLSTATE[42000]:
Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
I don't want to update mysql_strict mode in config/database.php
it's not working properly can anyone help me or any good suggestion or idea
Any help will appreciate
Can you figure out how to express this in Laravel?
**Schema (MySQL v5.7)**
CREATE TABLE my_table
(ID SERIAL PRIMARY KEY
,sender_id INT NOT NULL
,receiver_id INT NOT NULL
,message VARCHAR(50) NOT NULL
);
INSERT INTO my_table VALUES
(1,1,2,'Hello'),
(2,1,2,'Hey hello reply'),
(3,2,1,'Good'),
(4,3,2,'Hey GOOD Morning'),
(5,3,2,'good night');
---
**Query #1**
SELECT a.*
FROM my_table a
JOIN
( SELECT MAX(id) id
FROM my_table
WHERE 2 IN(sender_id,receiver_id)
GROUP
BY LEAST(sender_id,receiver_id)
, GREATEST(sender_id,receiver_id)
) b
ON b.id = a.id;
| ID | sender_id | receiver_id | message |
| --- | --------- | ----------- | ---------- |
| 3 | 2 | 1 | Good |
| 5 | 3 | 2 | good night |
---
View on DB Fiddle

Optimization of SQL with subquery and Having

Currently we are using a custom CI library to generate PDF files from documents which exist as database records in our database.
Each document is related to the contents (== rows) with a one-has-many relation. Each row has a number (field: row_span) to indicate how many lines it will use once it gets printed in the PDF.
Per PDF page that gets build, Rows needed for that page only are selected using a subquery:
$where = $this->docType."_id = ".$this->data['doc']->id." AND visible = 1";
$sql = "SELECT *,
(SELECT
sum(row_span) FROM app_".$this->docType."_rows X
WHERE X.position <= O.position
AND ".$where."
ORDER BY position ASC) 'span_total'
FROM app_".$this->docType."_rows O
WHERE ".$where."
HAVING span_total > ".(($i-1)*$this->maxRows)." AND span_total <= ".($i*$this->maxRows)." ORDER BY O.position ASC ";
$rows = $rows->query($sql);
In the code $i is the page number and $this->maxRows is loaded from the document template record which indicates how many available lines the PDF template has.
So when the SQL renders it might look like this for page 1 of an order with ID 834:
SELECT `app_order_rows`.*,
(SELECT SUM(`app_order_rows_subquery`.`row_span`) AS row_span
FROM `app_order_rows` `app_order_rows_subquery`
WHERE `app_order_rows_subquery`.`position` <= 'app_order_rows.position'
AND `app_order_rows_subquery`.`order_id` = 834
AND `app_order_rows_subquery`.`visible` = 1
ORDER BY `app_order_rows_subquery`.`position` asc) AS span_total
FROM (`app_order_rows`)
WHERE `app_order_rows`.`order_id` = 834
AND `app_order_rows`.`visible` = 1
HAVING span_total > 0
AND span_total <= 45
ORDER BY `app_order_rows`.`position` asc
And running this with EXPLAIN gives this as output:
+====+=============+=========================+======+===============+======+=========+======+======+=============================+===+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+====+=============+=========================+======+===============+======+=========+======+======+=============================+===+
| 1 | PRIMARY | app_order_rows | ALL | NULL | NULL | NULL | NULL | 1809 | Using where; Using filesort | 1 |
+----+-------------+-------------------------+------+---------------+------+---------+------+------+-----------------------------+---+
| 2 | SUBQUERY | app_order_rows_subquery | ALL | NULL | NULL | NULL | NULL | 1809 | Using where | 2 |
+====+=============+=========================+======+===============+======+=========+======+======+=============================+===+
This is working great, but... When we have large orders or invoices it renders the documents very slow. This might be due to the subquery.
Does anyone have an idea on how to do the same select without subquery? Maybe we will have to go for a whole new approach to select rows and build the PDF. We are open for suggestions ^^
Thanks in advance
------------------------------- edit ------------------------------
The EXPLAIN after index creation:
+====+=============+=========================+=======+===============+============+=========+=======+======+=============+===+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+====+=============+=========================+=======+===============+============+=========+=======+======+=============+===+
| 1 | PRIMARY | app_order_rows | ref | index_main | index_main | 5 | const | 9 | Using where | 1 |
+----+-------------+-------------------------+-------+---------------+------------+---------+-------+------+-------------+---+
| 2 | SUBQUERY | app_order_rows_subquery | range | index_main | index_main | 10 | NULL | 1 | Using where | 2 |
+====+=============+=========================+=======+===============+============+=========+=======+======+=============+===+
As you confirmed in the comments, the tables have no indexes.
The immediate solution would be:
create index index_main on app_order_rows (order_id, position);

PHP MySQL consolidate column where other column has duplicates

I have a MySQL table that has three columns, the first is a unique key (INT(11) AUTO_INCREMENT), the next is an indexed value (VARCHAR(255)) and the third is a description (TEXT). There are duplicate values in the second column, but each row has a different description. I want to remove all rows where the second column is duplicated but append each description of the same indexed value to the first instance the value, and breaking string with a semicolon and space.
For example, my table looks like this:
cid | word | description
------------------------------
1 | cat | an animal with wiskers
2 | cat | a house pet
3 | dog | a member of the canine family
4 | cat | a cool person
I want to change the table to look like this:
cid | word | description
------------------------------
1 | cat | an animal with wiskers; a house pet; a cool person
3 | dog | a member of the canine family
I'm not adverse to using a PHP script to do this, but would prefer MySQL. The table has over 170,000 rows and would take PHP a long time to loop over it.
SQL:
select `cid`,`word`,group_concat(`description` SEPARATOR '; ') as `description` from `test_table` group by `word`;
Ok.. you can copy all the data into another table, and rename it then..
insert into `test_new` (`cid`,`word`,`desc`) (select `cid`,`word`,group_concat(`desc` SEPARATOR '; ') as `description` from `test_table` group by `word`);
mysql> describe `test_new`;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| word | char(10) | YES | | NULL | |
| desc | text | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from `test_new`;
+------+------+---------------------+
| id | word | desc |
+------+------+---------------------+
| 1 | cat | desc1; desc2; desc4 |
| 3 | dog | desc3 |
+------+------+---------------------+
2 rows in set (0.00 sec)
As was mentioned before, you can create a new table and copy the info, you can also do it in two steps, but only if thereĀ“s no problem with modifying the old table:
UPDATE tableOld AS N1, tableOld AS N2
SET N1.description = concat(concat(N1.description,"; "),N2.decription))
WHERE N2.word = N1.word
insert into tableNew (cid,name,description)select * from tableOld group by word

MySQL takes toomuch time to display 1 million data

I'm working on an application with has millions of data, and I need to list all the data in a page. At first I was fetching all the data in a single query and the pagination was done in the client side. But that takes almost 15 minutes to load the complete set. So I changed the code for fetching 10 rows per request and the pagination was done on server side. But still the performance is not up to the mark. So what all things should do to get the data quickly or what is the best way to handle huge data.
My query to fetch data :
UPDATED :
SELECT w.work_order_id,
(SELECT CONCAT(user_fname, ' ', user_lname) FROM users WHERE user_id = w.created_by) AS created_by,
CASE w.assignee WHEN 0 THEN 'All' WHEN -1 THEN 'Unassigned' ELSE CONCAT(u.user_fname, ' ', u.user_lname) END AS assignee
FROM FiveVan_work_orders w
LEFT JOIN users u ON (u.user_id = w.assignee)
WHERE ( w.work_order_status != 'Deleted' && w.work_order_status != 'Closed') ORDER BY w.created_on DESC LIMIT 0,10;
I have created index for the pages and this is the result of explaining the query
+----+--------------------+-------+--------+-------------------+---------------+---------+-------------------------------+--------+------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+--------+-------------------+---------------+---------+-------------------------------+--------+------------------------------------------+
| 1 | PRIMARY | w | index | work_order_status | work_order_id | 790 | NULL | 340319 | Using where; Using index; Using filesort |
| 1 | PRIMARY | u | eq_ref | PRIMARY | PRIMARY | 4 | fivevan_loadtest.w.assignee | 1 | NULL |
| 2 | DEPENDENT SUBQUERY | users | eq_ref | PRIMARY | PRIMARY | 4 | fivevan_loadtest.w.created_by | 1 | NULL |
+----+--------------------+-------+--------+-------------------+---------------+---------+-------------------------------+--------+------------------------------------------+
WHERE ( w.work_order_status != 'Deleted' &&
w.work_order_status != 'Closed')
ORDER BY w.created_on DESC
If there is only one more value of work_order_status, then change that to
WHERE w.work_order_status = 'Open'
ORDER BY w.created_on DESC
and add
INDEX(work_order_status, created_on)
If there are multiple other values, the this may work (less well):
INDEX(created_on)
To get even better performance, you need to "remember where you left off" instead of using OFFSET. I discuss that in my pagination blog.

PHP Unset index if no match

I have two tables that have these columns (I'm only showing revelant ones) :
tasks table
+----+------+
| id | todo |
+----+------+
| 1 | 0 |
| 2 | 1 |
| 3 | 1 |
| 4 | 0 |
| 5 | 1 |
+----+------+
entries table
+----+---------+---------+------+
| id | task_id | user_id | done |
+----+---------+---------+------+
| 1 | 3 | 1 | 1 |
| 2 | 5 | 2 | 1 |
| 3 | 5 | 1 | 1 |
| 4 | 2 | 1 | 0 |
+----+---------+---------+------+
I query these tables and only keep tasks where todo = 1, So I already have the data in a PHP object.
I then have two lists that the user can view : tasks that are to do, and archived (done), and tasks that are to do. I can generate the first list just fine, I'm looping through each task and entries if they have a matching task_id where user_id == $loggeduser && done == 1, and unsetting the index of those that don't match. However, I cannot find a logic to do this with my archive list, as I don't have entries to match. How do I loop my tasks and only keep those that are done, for the user? In this case, for the archive list for user 1, I'm excepting to only keep task id 3 and 5, and for user 2, only keep task id 2.
Thanks.
You can do all this using plain SQL (I suppose you're using some relational database).
This query gives you all the tasks "todo & done". To get the tasks "todo & not done", just change the "e.done = 1" to "e.done = 0". I'm sure you get the idea.
SELECT * FROM tasks t
INNER JOIN entries e ON t.id = e.task_id
AND e.user_id = [logged_user_id]
AND e.done = 1
WHERE
t.todo = 0

Categories