I have a table that is something like this
id | names | value
1 Vicky 43
2 Erica 23
3 Rueben 33
4 Bob 54
5 Chris 60
Then I set them in order according to their value. Now the table looks like this.
id | names | value
5 Chris 60
4 Bob 54
1 Vicky 43
3 Rueben 33
2 Erica 23
Now the starting point is id 5 which has a name of Chris and a value of 60. My goal is, to get the next row which has an id of 4 and name of Bob and a value of 54.
You just need to limit the resultset:
SELECT * from table
ORDER BY value DESC
LIMIT 1, 1
Output:
| ID | NAMES | VALUE |
|----|-------|-------|
| 4 | Bob | 54 |
Fiddle here.
The LIMIT basically works this way: the first number sets the starting point (being 0 the minimal value) and the second number the amount of items to fetch (in this case only one).
Edit:
A different way of understanding the question would be: Given a value for a particular field (EG: id field with value of 5)... what would be the previous record? As we have the id 4 in the data we should return that one.
That could be accomplished this way:
SELECT * from t
WHERE id < 5
ORDER BY id DESC
LIMIT 1
Fiddle here.
This way you can traverse the results in both orders (ASC and DESC) and also get both the next or previous (> or <) rows.
If your current ID is for example 4 then
Next:
select * from foo where id = (select min(id) from foo where id > 4)
previous:
select * from foo where id = (select max(id) from foo where id < 4)
sql server:
with temp as
(
SELECT ROW_NUMBER() OVER (ORDER BY value desc) AS ROWID, * FROM table_name
)
SELECT * from temp where ROWID=2
mysql:
SELECT * from table
ORDER BY value DESC
LIMIT 1, 1
I get the feeling that this is a PHP related question?
If that's so, then you can use PHP's mysql or mysqli_fetch functions to get what you want... along with a loop
This is your basic loop-through-a-mysql-query
$sql = mysql_query( "SELECT * from table ORDER BY value DESC" );
while ( $r = mysql_fetch_array( $sql ) ) {
echo $r['value'] . "<br />\n";
}
If you want to have them all at your disposal and be able to call either one of them at will, you will need to store the data in an accessible array, like so
$sql = mysql_query( "SELECT * from table ORDER BY value DESC" );
$theData = array();
while ( $r = mysql_fetch_array( $sql ) ) {
$theData[] = $r['value'];
}
Then, to access the SECOND value, use this
echo $theData[1];
Related
So for example I have a table users, with a column 'count' and a column 'uid' which is the primary key.
uid | count
1 | 20
2 | 20
3 | 20
4 | 20
4 | 18
I want to select exactly one row which has count less than or equal to the present row. For example, I have the row where uid = 2.
Now I want to select a column which has count less than or equal to the present count value which is "20". and I want to select exactly one row which is closest to it.
Now I will have the choice to select either the row which has uid = 3 or uid = 4. In such case, I will want to select the column with the lowest uid value such that it is greater than the present uid value which is 2. Therefore I will want uid = 3 as my result.
How to put this in a mysql query ?
So something like this?
SELECT * FROM users
WHERE count <= 20
ORDER BY count DESC, uid ASC
LIMIT 1
That'll sort the results so that everything above 20 is discarded, and you get the rest in decreasing count order, with lower user ids taking priority if there are multiples of the same count. The LIMIT 1 restricts the query to return only one row.
If you want to make the comparison to an existing row, your easiest bet is to do this:
SELECT * FROM users
WHERE count <= 20
AND uid != 2
ORDER BY count DESC, uid ASC
LIMIT 1
SELECT * FROM users
WHERE count <= 20
AND uid IN (3,4)
ORDER BY uid ASC,count DESC
LIMIT 1
Table:
id value
100 1
101 1
102 1
102 0
103 1
I want the selection to return id's 100,101,103; that is, if the same id has two rows, one with value=1 and a second with value=0, I want to exclude it.
Appreciate any help.
Try this:-
Select id From table Group By id Having count(*) = 1
Could use the having MySQL construct:
SELECT id, count(*) AS tehCount FROM table GROUP BY id HAVING tehCount = 1;
Try this:
SELECT * FROM
table
GROUP BY id
HAVING COUNT(*) = 1
I have a simple table (product) with fields id, label.
I want to get the n-th unique row from it. If i query like this:
$query = 'SELECT * FROM product ORDER BY id ASC WHERE id ='.$n;
where $n is a number it will return the row that id = number but my table is something like this:
id | label
1 | sometext
4 | sometext
33 | sometext
40 | sometext
..
i mean, if i want to get the 3-th row which is ( 33 | sometext ) the query wont work.
Is there a simple way to get the 3-th or 4-th row ?
Thanks.
SELECT * FROM product ORDER BY id LIMIT 2,1;
This will get the 3rd row from the table.
2 is the start point
1 is the number of results returned
"SELECT * FROM product ORDER BY id ASC LIMIT 2,1"
will get you the third row
SELECT *
FROM
(SELECT *
FROM product
ORDER BY id ASC
LIMIT'.$n.' )
ORDER BY ID DSC
LIMIT 1
I'm not sure exactly how this is called but I'll try to describe as good as I can what I want to acheive.
So, first of all, there is a variable, called $id, which is actually $_GET['id']. Assuming the user is entering the following page by requesting: /page.php?id=6. Now what I need to do is to provide the information about the next 3 pages from database. Here is the database:
TABLE `pages`
id | page_name
______________________
1 | AAAAA
2 | BBBBB
3 | CCCCC
4 | DDDDD
5 | EEEEE
6 | FFFFF
7 | GGGGG
8 | HHHHH
9 | IIIII
So, while requesting the page with id 6, the following script returns the next 3 pages (7,8,9):
$res = mysql_query("SELECT * FROM `pages` WHERE `id`>'".intval($id)."' ORDER BY `id` DESC LIMIT 3");
while($arr = mysql_fetch_assoc($res))
{
print("Page ID: ".$arr['id']."; Page Name: ".$arr['page_name']."\n");
}
And here is the output:
Page ID: 7; Page Name: GGGGG
Page ID: 8; Page Name: HHHHH
Page ID: 9; Page Name: IIIII
And it works fine until the $id is greater then 6. When it is (/page.php?id={7/8/9}), the output doesn't show 3 pages any more, but 2 pages, 1 page and respectively no output when $id is 9.
So my question is: Is there a way to go back and start from the beginning when there are not enough results (less than 3) to display?
When accessing /page.php?id=8, the output should contain pages with id 9, 1 and 2.
When accessing /page.php?id=9, the output should contain pages with id 1, 2, 3.
When accessing /page.php?id=3, the output should contain pages with id 4, 5, 6 and so on.
(SELECT *, 0 AS custom_order FROM `pages` WHERE `id`>'".intval($id)."' ORDER BY `id` ASC LIMIT 3)
UNION ALL
(SELECT *, 1 AS custom_order FROM `pages` ORDER BY `id` ASC LIMIT 3)
ORDER BY custom_order, id ASC
LIMIT 3
This way you always get 3 pages. If not enough next pages, you will get up to 3 from the beginning.
You could modify the query to be something like:
select * from
(select *, id-$inval($id) as order_by
from pages were id > $inval($id) order by id asc limit 3
union
select *, id as order_by
from pages order by id asc limit 3 ) as pages
order by order_by asc
I would solve this way (one possible issue is that the resultset could contain at most 6 records instead of 3):
$res = mysql_query("(SELECT * FROM `pages` WHERE `id`>'".intval($id)."' ORDER BY `id` ASC LIMIT 3) UNION DISTINCT (SELECT * FROM `pages` WHERE id>0 ORDER BY id ASC LIMIT 3)");
$counter = 0;
while($arr = mysql_fetch_assoc($res) && $counter<3)
{
$counter++;
print("Page ID: ".$arr['id']."; Page Name: ".$arr['page_name']."\n");
}
I have a table, which consists of 3 fields:
id
name
status
Every time I get the results, it should give me 5 names whose status = 1.
Suppose the db contains following:
id name status
1 A 1
2 B 1
3 C 0
4 D 1
5 E 0
6 F 0
7 H 1
8 I 1
9 J 1
10 K 1
11 L 1
12 M 0
1st time, fetch should return: A,B,D,H,I (5 records)
2nd time, fetch should return: J,K,L,A,B (5 records)
UPDATE: I don't want typical pagenation. Consider I have 12 available names from A1 to A12. The first fetch should return A1-A5, second fetch A6-A10 and third fetch A11, A12, A1, A2, A3. So when I reach the end, I need to get records starting from the first to fill the 5 slots.
i am doing it in php with mysql
This looks like some sort of job allocation script?
You need 2 things:
the highest ID returned last time the script was run (lastID)
a number larger than the maximum ID in the table (bigNum)
Then you can write your query as
SELECT
id, name
FROM
table
WHERE
status=1
ORDER BY
(bignum + id) MOD (bigNum + lastID + 1)
LIMIT 5
Shazaam!
Keep track of the ids of the records returned, and for the following queries do:
select top 5 *
from (
select top 5 *
from MyTable
where status = 1
and id not in (1,2,4,7,8)
order by name
union
select top 5 *
from MyTable
where status = 1
order by name
) a
$q = mysql_query("SELECT name FROM table WHERE status = 1 LIMIT 5);
while ($row = mysql_fetch_row($q))
{
.... //first 5
}
$q = mysql_query("SELECT name FROM table WHERE status = 1 LIMIT 5,5);
while ($row = mysql_fetch_row($q))
{
.... //second 5
}
this uses the offset functionality of mysql- think of it as pagination for your results.