I have a simple question regrading MySQL. Is it possible to return the rows between row 'x' and row 'y'? It's sort of hard to explain - for the sake of an example: Return rows 6 through 10, excluding rows 1-5 and rows 11+. Thanks! ;D
Use LIMIT. Remember to combine it with ORDER BY for the results to make any sense.
SELECT fields, ...
FROM table
ORDER BY something_sensible
LIMIT 5, 5
(Start from row 6, take 5 rows)
SELECT * FROM table LIMIT 5, 5
http://dev.mysql.com/doc/refman/5.5/en/select.html and look at LIMIT section
Yes, here's an example:
SELECT * FROM myTable LIMIT 5, 5
From the manual (http://dev.mysql.com/doc/refman/5.0/en/select.html):
The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
mysql> select * from employees order by emp_id;
+--------+-----------------+---------+
| emp_id | name | boss_id |
+--------+-----------------+---------+
| 1 | f00 | NULL |
| 2 | ali later | 1 |
| 3 | megan fox | 1 |
| 4 | jessica alba | 3 |
| 5 | eva longoria | 3 |
| 6 | keira knightley | 5 |
| 7 | liv tyler | 6 |
| 8 | sophie marceau | 6 |
+--------+-----------------+---------+
8 rows in set (0.00 sec)
mysql> select * from employees order by emp_id limit 2,4;
+--------+-----------------+---------+
| emp_id | name | boss_id |
+--------+-----------------+---------+
| 3 | megan fox | 1 |
| 4 | jessica alba | 3 |
| 5 | eva longoria | 3 |
| 6 | keira knightley | 5 |
+--------+-----------------+---------+
4 rows in set (0.00 sec)
Why don't you use an Auto Increment field? Or you can use LIMIT keyword like:
SELECT * FROM tablename WHERE LIMIT 0, 5
This will show records 1,2,3,4,5
Related
So my table looks like this:
| id | user | points |
| 1 | Sam | 1 |
| 2 | Sam | 6 |
| 3 | Phil | 1 |
The query I am currently using is:
SELECT user,COUNT(*) FROM table GROUP BY user order by COUNT(*) DESC
This returns the current value:
Sam: 2
Phil: 1
It looks like it counts the number of rows, not the total points? How can I do this?
The correct return should be Sam: 7.
Use SUM instead of COUNT
SELECT user, SUM(points) FROM table GROUP BY user
I have a table as following:(Ex)
id | vid | time
------------------------
1 | 4 | 1333635317
2 | 4 | 1333635323
3 | 2 | 1333635336
4 | 4 | 1333635343
5 | 5 | 1333635349
I want to be just a row (the last row [ID: 4]) of the same rows[id:1,2,4], how it will output the query?
I mean, as a result of these:
id | vid | time
------------------------
3 | 2 | 1333635336
4 | 4 | 1333635343
5 | 5 | 1333635349
What do i do?
i trying it as:
SELECT * from tbale as t1 where vid = 4 GROUP BY vid ORDER BY id DESC
but doesn't work ORDER BY in my query.
Get the max time per vid and use in to get those rows from the table.
select * from tablename
where (vid,time) in (select vid,max(time)
from tablename
group by vid)
order by id
Hi I am trying to create something but I can't have my total number being a negative. So basically I got values that are negative that can be multiplied into a somewhat complex mathematics equation.
So essentially... user inputs a data from -1 to 1. (-1, 0, 1)
And it get's multiplied into my formula. So my SQL query looks like this... (this part works!)
SELECT *, a, b, c AS TOTALNUMBER FROM MATH
ORDER BY TOTALNUMBER DESC
However, I need the total number to always be positive. So I have been trying for the past few hours to figure this out. I am sort of new to php/sql.
I am trying to include something like...
if (TOTALNUMBER < 0 ) {
TOTALNUMBER * -1.0
}
However I have no idea where to include this in the query or how to write it properly.
To clarify and update what I am looking for...
User can input -1,0,1
Data for A, B, C is for example. 10, 15, 20
User inputs: 1, -1, 0
A total = 10
B total = -15
C total = 0
Total ABC = -5
However, I need total to be 5 instead of -5 without changing any A, B, C values.
use like this
SELECT *, a, b, ABS(c) AS TOTALNUMBER FROM MATH
ORDER BY TOTALNUMBER DESC
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used.
SELECT a, b, c, If (c <0 , c * -1, c) AS TOTALNUMBER FROM MATH
ORDER BY TOTALNUMBER DESC;
If you want the sum of all the fields to be your total number, assuming you have an id field:
SELECT a,b,c,ABS((sum(a)+(b)+(c))) AS TOTALNUMBER FROM MATH
group by mathid ORDER BY TOTALNUMBER DESC;
Here is an example:
mysql> select * from math;
+--------+----------+
| idmath | mathcol1 |
+--------+----------+
| 1 | 1 |
| 2 | 3 |
| 3 | -1 |
| 4 | -3 |
+--------+----------+
4 rows in set (0.00 sec)
mysql> SELECT idmath, If (mathcol1 <0 , mathcol1 * -1, mathcol1) AS TOTALNUMBER
FROM MATH ORDER BY TOTALNUMBER DESC;
+--------+-------------+
| idmath | TOTALNUMBER |
+--------+-------------+
| 2 | 3 |
| 4 | 3 |
| 1 | 1 |
| 3 | 1 |
+--------+-------------+
4 rows in set (0.00 sec)
mysql> SELECT idmath, mathcol1, If (mathcol1 <0 , mathcol1 * -1, mathcol1) AS TO
TALNUMBER FROM MATH ORDER BY TOTALNUMBER DESC;
+--------+----------+-------------+
| idmath | mathcol1 | TOTALNUMBER |
+--------+----------+-------------+
| 2 | 3 | 3 |
| 4 | -3 | 3 |
| 1 | 1 | 1 |
| 3 | -1 | 1 |
+--------+----------+-------------+
4 rows in set (0.00 sec)
mysql> SELECT mathcol1,mathcol2,mathcol3, (sum(ABS(mathcol1))+(mathcol2)+(mathco
l3)) AS TOTALNUMBER FROM MATH group by idmath ORDER BY TOTALNUMBER DESC;
+----------+----------+----------+-------------+
| mathcol1 | mathcol2 | mathcol3 | TOTALNUMBER |
+----------+----------+----------+-------------+
| 3 | 2 | 3 | 8 |
| -3 | 2 | 3 | 8 |
| -1 | 2 | 3 | 6 |
| 1 | 2 | 3 | 6 |
+----------+----------+----------+-------------+
4 rows in set (0.00 sec)
You can read more here at dev.mysql.
I have a table of 16K entries
I want to extract random 44 entries
but I don't want to repeat the same entries more then once (ever)
so i have a per-user list that keeps the already used 'IDs' as a comma-separated string in a table.
and I use that list to SELECT ... NOT IN (used_IDs)
The issue is that this list is getting too big and the sql call fails because of size i believe
Any idea on how to do that more usefully?
Questions table:
+------+-------+-------+
| id | Qtext | Tags |
+------+-------+-------+
Test table:
+------+-------+
| id | QIDs |
+------+-------+
Results table:
+------+-------+-------+
| id | tID | uID |
+------+-------+-------+
I need to select unique random values from Questions table based on the results table. (which associates test ID with Question IDs)
Currently trying to use:
SELECT DISTINCT `questions`.`ID`
FROM `questions`, `tests`, `results`
WHERE
`questions`.`ID` NOT IN (`tests`.`qIDs`)
AND `results`.`uID` = 1 AND `tests`.`ID` = `results`.`tID`
AND 4 IN ( `questions`.`tags`)
AND "http://www.usmlestep2qna.com" = `provider`
ORDER BY RAND() LIMIT 27;
Any ideas?
Instead of placing the used user Id values in a comma-separated string in one column, you could create a tall table to store them. This should yield better preformance
Rather than using a single row with a (potentially huge) CSV, why not use a nicely indexed table and an outer join to pick unmatched records. I have an example from my test database:
mysql> select * from first;
+------+-------+
| id | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
| 4 | NULL |
| 6 | gggg |
+------+-------+
5 rows in set (0.00 sec)
mysql> select * from second;
+------+----------+------+------+-------+------+
| id | first_id | one | two | three | four |
+------+----------+------+------+-------+------+
| 1 | 1 | 3 | 0 | 4 | 6 |
| 1 | 2 | 4 | 4 | 1 | 2 |
| 3 | 3 | 1 | NULL | 3 | 4 |
+------+----------+------+------+-------+------+
3 rows in set (0.00 sec)
mysql> select a.id from first a join second b on a.id=b.first_id;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)
mysql> select a.id from first a
left outer join second b on a.id=b.first_id where b.first_id is null;
+------+
| id |
+------+
| 4 |
| 6 |
+------+
2 rows in set (0.00 sec)
This should improve your performance rather nicely.
i have a mysql table i.e
st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
my question is how can i find the highest subject score, the second last and so on
Note that all the subject fields have int(11) values
Break your database into 3 tables like:
Students:
st_id | name | email
1 | john |#a.com
Courses:
cr_id | name
1 | maths
2 | chemistry
3 | bio
4 | social_studies
StudentCourses:
st_id | cr_id | score
1 | 1 | 20
1 | 2 | 23
1 | 3 | 10
1 | 4 | 15
Now you can do:
SELECT s.name, MAX(sc.score) FROM Students s INNER JOIN StudentCourses sc ON s.st_id = sc.st_id;
SELECT * FROM <table>
ORDER BY <field> DESC
LIMIT <needed number of rows>
Example:
SELECT * FROM <table>
ORDER BY maths+chemistry+bio+social_study DESC
LIMIT 3
Strictly PHP method: I assume you want to maintain association with field names. In that case, just use asort($row); on each row in your query result, assuming you fetched the row as an array. asort will sort the array from lowest value to highest (with additional flags to tweak the results if needed), while maintaining keys. A foreach loop will then allow you to work with each key/value pair in the sorted order.
st_id | name | email | maths | chemistry | bio | social_study
1 | john |#a.com | 20 | 23 | 10 | 15
The query can be for top marks
SELECT id,GREATEST(mark,mark1,mark2) AS `top` FROM `students`