I have a MySQL table with a million rows. The result of my query needs to contain 100 rows and has to be ordered by date.
I would now like to add a clause to the query that makes it possible to tell the database to "start the result with the row that comes after the row with the id '5'", for example. My result would therefore include the rows with the ids 4, 3, 2, 6, 8, ...
Are you, dear reader, the sought database wizard who could put me out of my misery? ;)
Query:
SELECT id, type, content, date
FROM my_table
WHERE type = 'FI'
ORDER BY date ASC
LIMIT 100;
Result:
| id | type | content | date |
|----|------|----------|------------|
| 7 | FI | ContentR | 2014-01-01 |
| 9 | FI | ContentS | 2014-01-13 |
| 1 | FI | ContentT | 2014-02-09 |
| 5 | FI | ContentU | 2014-03-27 |
| 4 | FI | ContentV | 2014-03-30 | ---|
| 3 | FI | ContentW | 2014-04-01 | |
| 2 | FI | ContentX | 2014-06-10 | |- The result I want
| 6 | FI | ContentY | 2014-09-03 | |
| 8 | FI | ContentZ | 2014-12-09 | |
... ---|
SELECT id, type, content, date
FROM my_table
WHERE type = 'FI'
AND date > (SELECT date FROM my_table WHERE id = 5)
ORDER BY date ASC
LIMIT 100;
Related
I need to get records of 2 tables using filters and sorting.
Tables: ra_pedidos, ra_pedido_has_pedido_status
It worked perfectly in MySQL5.0, but now in 5.5 it does not bring the correct result anymore.
I need in the same line to bring the date of the first status and last change of status of each request and ignore the canceled ones.
Example search (ra_pedido_has_pedido_status):
SELECT id_pedido, id_pedido_status, data
FROM ra_pedido_has_pedido_status
WHERE id_pedido IN (86291, 86745, 86622);
Result:
+-----------+------------------+---------------------+
| id_pedido | id_pedido_status | data |
+-----------+------------------+---------------------+
| 86291 | 1 | 2017-10-30 11:16:12 |
| 86291 | 2 | 2017-10-30 14:14:53 |
| 86291 | 3 | 2017-10-31 08:18:47 |
| 86291 | 11 | 2017-11-07 12:08:04 |
| 86622 | 1 | 2017-11-04 12:23:21 |
| 86622 | 2 | 2017-11-04 12:47:33 |
| 86622 | 3 | 2017-11-06 08:24:20 |
| 86622 | 90 | 2017-11-07 08:40:55 |
| 86745 | 1 | 2017-11-07 10:59:51 |
| 86745 | 2 | 2017-11-07 11:09:46 |
+-----------+------------------+---------------------+
Now report SQL:
[EDIT] Explain: JOIN get first match row, then the ORDER BY on SELECT inside this join force to get last row. Note that id_pedido 86622 should not list because it is canceled (last id_pedido_status = 90), and this is filtered in HAVING
SELECT o.id_pedido AS id, pus.id_pedido_status, pus.data
FROM ra_pedido AS o
INNER JOIN
(
SELECT ra_pedido_has_pedido_status.*
FROM ra_pedido_has_pedido_status
ORDER BY data DESC
) AS pus ON o.id_pedido = pus.id_pedido
WHERE o.id_pedido IN (86291, 86745, 86622)
GROUP BY o.id_pedido
HAVING id_pedido_status < 90
ORDER BY data DESC;
This is the result using MySQL 5.0, the expected:
+-------+------------------+---------------------+
| id | id_pedido_status | data |
+-------+------------------+---------------------+
| 86291 | 11 | 2017-11-07 12:08:04 |
| 86745 | 2 | 2017-11-07 11:09:46 |
+-------+------------------+---------------------+
And this is the result using MySQL 5.5, not expected:
+-------+------------------+---------------------+
| id | id_pedido_status | data |
+-------+------------------+---------------------+
| 86745 | 1 | 2017-11-07 10:59:51 |
| 86622 | 1 | 2017-11-04 12:23:21 |
| 86291 | 1 | 2017-10-30 11:16:12 |
+-------+------------------+---------------------+
What do I need to change in SQL?
Thanks and sorry my english...
I need a little bit of help with making a complicated query, i will try to explain what i am trying to accomplish down below.
Here is my data
table_one
+---------+---------+----------+----------+
| user_id | poly_id | in | out |
+---------+---------+----------+----------+
| 900 | 1 | 20-12-17 | 20-12-17 |
| 900 | 2 | 21-12-17 | 22-12-17 |
| 900 | 3 | 22-12-17 | 24-12-17 |
| 900 | 1 | 23-12-17 | 26-12-17 |
| 444 | 4 | 24-12-17 | 28-12-17 |
| 444 | 4 | 25-12-17 | 30-12-17 |
| 444 | 5 | 26-12-17 | 01-01-18 |
| 444 | 3 | 27-12-17 | 03-01-18 |
| 900 | 2 | 28-12-17 | 05-01-18 |
| 900 | 1 | 29-12-17 | 07-01-18 |
| 444 | 2 | 30-12-17 | 09-01-18 |
+---------+---------+----------+----------+
table_two
+----+---------+-------------+---------+
| id | name | type | product |
+----+---------+-------------+---------+
| 1 | city 1 | gas station | general |
| 2 | city 2 | workshop | general |
| 3 | city 3 | paint | bikes |
| 4 | city 4 | paint | general |
| 5 | city 5 | gas station | cars |
| 6 | city 6 | gas station | bikes |
| 7 | city 7 | paint | cars |
| 8 | city 8 | workshop | cars |
| 9 | city 9 | gas station | general |
| 10 | city 10 | gas station | cars |
| 11 | city 11 | gas station | general |
+----+---------+-------------+---------+
i have a working solution that looks like this
//results comes from somewhere else, it looks something like this for example:
array (
"user_id" => "poly_id of the last gas station"
"900" => 1,
"444" => 10
)
foreach ($result AS $res ) {
$query = "
SELECT
table_one.name AS name
FROM
`table_one`
LEFT JOIN
`table_two` ON table_one.poly_id = table_two.id
WHERE
`table_two`.type = 'gas station '
AND
table_one.user_id = $res['user_id']
AND
table_one.poly_id != $res['poly_id']
AND
table_one.in >= "some date from'
AND
table_one.out <= 'some date to'
AND
(FIND_IN_SET('general', table_two.product) > 0 OR FIND_IN_SET('cars', table_two.product) > 0 )
ORDER BY out DESC LIMIT 1
";
//if the results is not empty use the result['name']
}
The idea is: I have the user last gas station, but i need to find the previous one between a date range.
As i said, the above example is working just fine, however i need to be able to process multiple results at once, and sometimes the results are ~2000.
Which means 2000+ queries per request.
Is it even possible to somehow optimize this loop of queries into a single one, so I don't do 2000 queries per request ?
If possible, how :D
Thanks.
This query will return result contains user_id with it's last
enter in the given periode.
one thing here because you are
using date not date time if the user exit in the same
day two time you will have two record of that user you can skip the second
record in you code
select
user_id,
in,
out
from table_one t1
INNER JOIN (
select
user_id
max(out) as 'max_out',
from
table_one
where
in >= ? AND
out <= ? AND
ploy_id not in [list_of_unwanted_ploy_id]
-- you can specify any condition here
group by user_id
) l_out on t1.user_id = l_out.user_id and t1.out = l_out.max_out
where
t1.user_id in [list_of_user_id]
Following query gives result-set for available months only , how can i get for all the months if data is not present .
Eg:-
Apr-2013 and May-2013 having the value but June-2013 no value how can i get June-2013 in result set as total is 0 .
SELECT CONCAT(substr(MonthName(fileddate) , 1, 3) , "-", Year(fileddate)) AS MONTH,
count(*)
FROM carstore
WHERE store=20
AND (soldstate = 2
OR soldstate = 3)
AND cartype LIKE '%Toyoto%'
AND fileddate BETWEEN DATE('2013-04-07') AND DATE('2014-04-30')
GROUP BY MONTH
ORDER BY Year(fileddate),
MONTH(fileddate) ;
+----------+----------+
| Month | count(*) |
+----------+----------+
| Apr-2013 | 2 |
| May-2013 | 2 |
| Jul-2013 | 14 |
| Aug-2013 | 3 |
| Sep-2013 | 2 |
| Nov-2013 | 4 |
| Dec-2013 | 19 |
| Jan-2014 | 61 |
| Feb-2014 | 21 |
| Apr-2014 | 3 |
+----------+----------+
I am writing a program in java to fetch records from infobright database. Here are the table structure, query and the format of output I require.
Table structure:
column name type
------------ ----------
id integer
date integer
product_id integer
search_engine_id integer
visitor integer
example query:
select date,
product_id,
search_engine_id,
sum(visitor)
from report
where date in (201300910, 20130919)
group by search_engine_id,
product_id,
date
order by product_id,
date
The result set of the above query would be in the following format:
+----------+-------------+----------------+------------------+
| date | product_id | search_engine_id | sum(visitor) |
+----------+-------------+----------------+------------------+
| 20130910 | 108 | 2 | 7 |
| 20130910 | 108 | 1 | 15 |
| 20130919 | 108 | 1 | 2 |
| 20130919 | 108 | 2 | 3 |
| 20130910 | 107 | 1 | 3 |
| 20130910 | 107 | 2 | 2 |
| 20130919 | 107 | 1 | 2 |
| 20130919 | 107 | 2 | 3 |
| 20130919 | 106 | 1 | 10 |
There are two types of search_engine_id, that is 1 and 2.
For a given day, one product_id can exist two times at max, i.e., one record for search_engine_id 1 and other record for search_engine_id 2. What I want is to group the result with respect to product_id. So it would look something like:
+-------------------+-------------+----------------+------------------+
| date | product_id | search_engine_id | sum(visitor) |
+-------------------+-------------+----------------+------------------+
| 20130910,20130919 | 108 | 2,1,1,2 | 7,15,2,3 |
The main reason of doing this in the sql side is to make the data prepared for every product_id so that it will have less memory usage in java side. I have tried group_contact function but that seems to be not supported by infobright. Is there a way I can rewrite the query/make use of some other function to achieve this?
I've got a table that looks like:
Table 1 ->
+----+--------+--------+
| id | name | author |
+----+--------+--------+
| 1 | First | Me |
| 2 | Second | You |
+----+--------+--------+
Table 2 ->
+-----+------------+-----------+------------+
| mid | table1_id | key | value |
+-----+------------+-----------+------------+
| 1 | 1 | desc | hello |
| 2 | 1 | begin_day | monday |
| 3 | 1 | end_day | tuesday |
| 4 | 2 | desc | goodbye |
| 5 | 2 | begin_day | wednesday |
| 6 | 2 | end_day | friday |
+-----+------------+-----------+------------+
The relationship here is that the id in table 1 corresponds to the table1_id in table 2.
The output that I'm trying to get is...
+----+---------+---------+-------------+-----------+-----------+
| id | name | author | desc | begin_day | end_day |
+----+---------+---------+-------------+-----------+-----------+
| 1 | First | Me | hello | monday | tuesday |
| 1 | Second | You | goodbye | wednesday | friday |
+----+---------+---------+-------------+-----------+-----------+
I've tried several different join statements -- all a variation of the below. I'm not that well versed in MySQL queries, however.
SELECT * FROM table_1 LEFT JOIN table_2 on table_1.id = table_2.table1_id
Which produces...
+----+----------+----------+----------+------------+-----------+
| id | mid | name | author | key | value |
+----+----------+----------+----------+------------+-----------+
| 1 | 1 | First | Me | desc | hello |
| 1 | 2 | First | Me | begin_day | monday |
| 1 | 3 | First | Me | end_day | tuesday |
| 2 | 4 | Second | You | desc | goodbye |
| 2 | 5 | Second | You | begin_day | wednesday|
| 2 | 6 | Second | You | end_day | friday |
Obviously, iterating over this join statement produces 6 results, 1 for each row in table 2 that matches the id in table 1. How can I avoid this with a proper query statement?
Thank you in advance.
You can use a case statement if you know all of the columns you will be getting, as follows:
Select distinct table_1.*,
case when table_2.key='desc' then value end as desc,
case when table_2.key='begin_day' then value end as begin_day,
case when table_2.key='end_day' then value end as end_day
FROM table_1 LEFT JOIN table_2 on table_1.id = table_2.table1_id
Hope this helps!
SELECT
table_1.*,
MAX(IF(key='desc', value, NULL)) AS 'desc',
MAX(IF(key='begin_day', value, NULL)) AS begin_day,
MAX(IF(key='end_day', value, NULL)) AS end_day
FROM table_1
LEFT JOIN table_2 ON (id = table1_id)
GROUP BY id;