Hey I need to make a MySql query and get from it some number of user activities, lets say 10, then after scrolling on page I need to take another portion of activities stored in DB and start from 10 to 20 and so on... As I made this already by loading the whole DB Content for user and then dynamically show it with AJAX and jQuery I need to change the method I am doing this. So my query looks like this:
SELECT some rows FROM table WHERE User_ID = #memberID ORDER By date LIMIT limit
As this query works to take only limited records from DB I have no idea how to make a parameter that would determine which records should we take now. The problem starts when user refreshes the page - we want to start from 0 and again go 10 by 10 down.
EDIT: I am giving the query 2 params (LIMIT and OFFSET) and then in jQuery function gonna try to increase both of them.
you can do it like this
example
mysql> SELECT * FROM MAXWELL;
+------+-------+
| ID | NAME |
+------+-------+
| 3 | TWO |
| 4 | FOUR |
| 5 | FIVE |
| 6 | SIX |
| 7 | SEVEN |
+------+-------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM MAXWELL limit 0,2;
+------+------+
| ID | NAME |
+------+------+
| 3 | TWO |
| 4 | FOUR |
+------+------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM MAXWELL limit 2,4;
+------+-------+
| ID | NAME |
+------+-------+
| 5 | FIVE |
| 6 | SIX |
| 7 | SEVEN |
| 10 | ten |
+------+-------+
4 rows in set (0.00 sec)
Related
Now my problem is to know a mysql query will fetch result which exceeds a certain row count (like 5000 rows). I know it can use select * ... limit 5001 to replace count() for performance optimization in terms of time effeciency, but it still return 5001 row of records which is totally useless in my scenario, becasue all I want is a sample 'yes/no' answer. Is there any better approach? big thanks ! ^_^
The accepted answer in the link provided by Devsi Odedra
is substantially correct but if you don't want a big result set select a column into a user defined variable and limit 1
for example
MariaDB [sandbox]> select * from dates limit 7;
+----+------------+
| id | dte |
+----+------------+
| 1 | 2018-01-02 |
| 2 | 2018-01-03 |
| 3 | 2018-01-04 |
| 4 | 2018-01-05 |
| 5 | 2018-01-06 |
| 6 | 2018-01-07 |
| 7 | 2018-01-08 |
+----+------------+
SELECT SQL_CALC_FOUND_ROWS ID INTO #ID FROM DATES WHERE ID < 5 LIMIT 1;
SELECT FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
| 4 |
+--------------+
1 row in set (0.001 sec)
SELECT 1 FROM tbl
WHERE ... ORDER BY ...
LIMIT 5000, 1;
will give you either a row or no row -- This indicates whether there are more than 5000 row or not. Wrapping it in EXISTS( ... ) turns that into "true" or "false" -- essentially the same effort, but perhaps clearer syntax.
Caution: If the WHERE and ORDER BY are used but cannot handled by an INDEX, the query may still read the entire table before getting to the 5000 and 1.
When paginating, I recommend
LIMIT 11, 1
to fetch 10 rows, plus an indication that there are more rows.
I am writing an order fulfillment system in using a MySQL Server which pre-packaged boxes have been filled with items and I need to determine which, if any, combination of boxes to ship to satisfy an order. I am unsure what SQL tools are available to me to efficiently solve query for the boxes required.
An table of order items looks like the following, if I had order 1 be for a fork and spoon:
mysql> select * from order_items;
+----+-------+----------+
| id | name | order_id |
+----+-------+----------+
| 1 | fork | 1 |
| 2 | spoon | 1 |
+----+-------+----------+
2 rows in set (0.00 sec)
while the boxes are arranged as
mysql> select * from boxes;
+----+------+
| id | name |
+----+------+
| 1 | box1 |
| 2 | box2 |
| 3 | box3 |
+----+------+
3 rows in set (0.00 sec)
and
mysql> select * from items;
+----+-------+--------+
| id | name | box_id |
+----+-------+--------+
| 1 | spoon | 1 |
| 2 | knife | 1 |
| 3 | fork | 1 |
| 4 | knife | 2 |
| 5 | fork | 2 |
| 6 | spoon | 3 |
| 7 | knife | 3 |
+----+-------+--------+
7 rows in set (0.00 sec)
As you can see, there is a problem. It could be that boxes may contain an individual fork and another an individual spoon, or both in one box. However, in this case I have boxes with all three utensils, or a mixture of each. It is expected that, in general, a single box will not cover all the requirements for the order but it is acceptable to send a box with extra items if needbe. In this case, a single box would work OR the combination of the other two available boxes. In either case one or two extra knives would be sent. Ideally, we would like to send the minimal number of extra utensils but we do not care about the number of boxes.
What is the appropriate query to efficiently determine what combination of boxes will work? I do not know of a single query, but I think that a series of queries that try and find a match for all N items, then N-1, then N-2 until a match is found and then repeat for the remaining items. This seems fairly inefficient, though.
Edit:
The problem is to find a subset of all boxes B_S such m_i is a member of B_S for all items m_i in order M.
I have a table with a bit over a million timestamped rows, is there a way for me to select like 30 rows which are evenly distributed?
So that if my data table contains five rows and I need three I want row 1, 3 and 5 returned.
Is there a way to do this in SQL?
Edit:
More specifically, I have a table with a list of different URLs and another table where data about the URLs are fetched and stored with regular intervals (in my case hourly).
What I want to do is be able to fetch a limited number of data rows (in my case 30) with an even interval between the dates. In a sense I want to filter out data points at a dynamic interval.
Does that make sense?
I guess you could consider something like this..
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
Now let's say I wanted to return approximately 5 evenly distributed results from across this table...
SELECT x.i
FROM ints x
JOIN ints y
ON y.i <= x.i
GROUP
BY i
HAVING MOD(COUNT(y.i),ROUND((SELECT COUNT(*)/5 FROM ints),0)) = 0; -- where '5' equals the approximate number of results to be returned.
+---+
| i |
+---+
| 1 |
| 3 |
| 5 |
| 7 |
| 9 |
+---+
Note that at ca. 1m results, this solution is NOT going to scale well. Use variables for the ranking bit instead.
I have a query that retrieves some data, among those data I have some that are returned with a value like 0. I would like the query to NOT return the columns when that's the case.
How can we do such a thing?
Regards,
MEM
select <column_name> from <table_name> where <column_name> <> 0.0
Here is all the data in a sample database. Notice how there are 3 rows with one having a zero value for the num column.
mysql> select * from test_tbl;
+------+----------+
| num | some_str |
+------+----------+
| 0 | matt |
| 2 | todd |
| 3 | Paul |
+------+----------+
3 rows in set (0.00 sec)
Now lets use the where clause to specify the rows we want to ignore (it's a little bit of reverse logic because we are actually specifying what rows we want).
mysql> select * from test_tbl where num <> 0.0;
+------+----------+
| num | some_str |
+------+----------+
| 2 | todd |
| 3 | Paul |
+------+----------+
2 rows in set (0.00 sec)
Note: This will only work without getting messy if 0 is the only value you are worried about. A better way would be to allow nulls in your column and then you can check to see if they are non-null in the where clause.
I want to copy profiling data to a table I create in MySql.
I want the table to contain the exact data that I get from the command SHOW PROFILES;
For example, if I have this:
mysql> show profiles;
+----------+------------+--------------------------------+
| Query_ID | Duration | Query |
+----------+------------+--------------------------------+
| 16 | 0.00059700 | select * from imprumuturi |
| 17 | 0.00042050 | select * from imprumuturi |
| 18 | 0.00042000 | select * from imprumuturi |
| 19 | 0.00042950 | select * from imprumuturi |
| 20 | 0.00048050 | select * from imprumuturi |
+----------+------------+--------------------------------+
5 rows in set (0.00 sec)
I will create a table that has 3 columns (queryid,duration and query), and I need a command that will copy those 5 rows from "SHOW PROFILES;" to my table.
insert into table (show profiles); does not work
I need it to be done in MySql alone, no other tools/patches.
INSERT INTO t (SELECT some columns FROM INFORMATION_SCHEMA.PROFILING)
insert into t (select * from information_schema.profiling)