Hey, so what I am looking to accomplish is a MySQL query which grabs just the FIRST row whos ID is less than my current one. So basically a next button. Here is what I would like to modify:
$next = mysql_query("select * from posts where id<'$id'");
So how would I change this so it only selects the first value which is less than my ID value (which is the id of my current page)
Thanks! I will be doing this the reverse way for a previous button too.
select * from posts where id<'$id' order by id desc limit 1
This will get you the first id smaller than the provided parameter $id.
$next=mysql_query("SELECT * FROM posts WHERE id<'{$id}' ORDER BY id LIMIT 1");
Related
I am having trouble in finding the right code for my website. I want to fetch random row from a SQL data base using a PHP code and also I want the code to select any random row except the row with a particular ID.
This is the code that I came up with:
<?php
$sql = "SELECT column FROM table ORDER BY RAND() LIMIT 1";
?>
I want this code to select any row except the row whose id = 1.
You can try something like this:
SELECT * FROM table_name
where id <> 1
ORDER BY RAND()
LIMIT 1;
Trying to put only the value of column id from table existence:
$sql_query_existence = "SELECT * FROM existence ORDER BY id DESC LIMIT 1";
$sql_test = $sql_query_existence['id'];
The second line is my idea, how do I do this right? Any suggestion is appreciated.
So you want to get the value of the id column? If so, use this query
SELECT id FROM existence ORDER BY id DESC LIMIT 1;
and if you want to set the value of the id column only, use either
INSERT INTO existence(id) VALUES (<Your ID>);
or
UPDATE existence SET id=<Your ID> WHERE ...;
This answer is SQL based since I don't know php, but this answer will help you do the necessary php work involved.
I'm doing an info system that has announcements, so I wanted to get the data from announce table, first should be the one with max id of course since it is the latest announcement and here is my code for it:
SELECT image FROM announce where id=(SELECT max(id) FROM announce);
Now I wanted to get the data before the max id and I'm using this code here:
SELECT image FROM announce where id=(SELECT max(id)-1 FROM announce);
But it only works if there are no deleted rows,if there are some deleted rows it doesn't work like max id is 10 and the id's 9,8,7 are deleted. That means the present id's are: 1,2,3,4,5,6,10, how will I be able to get the data in the id 6,5,4,3,2,1?
If you ORDER the query by descending order (DESC), and have an OFFSET of one (OFFSET 1), you would get the second to last row. And offset of two would give the row before that, and so on. This also uses LIMIT 1, so we just get one row. LIMIT 1, 1 is the same as LIMIT 1 OFFSET 1.
SELECT image
FROM announce
ORDER BY id DESC
LIMIT 1, 1
MySQL on SELECT statements
You can just use one query with order by and limit instead of the two queries you use:
select image
from announce
order by id desc
limit 2
In case you want to query them separately, you can change limit 2 to limit 1, 1 for you second query to work as you wish.
i have a wordpress database table called stats that saves records of one post, actually the data saved is the post score, so i might have post id 1 with score 49, another record for post id 2 with score 59, but the post id is the same, so my question is this,
how will i get the score of the second last post id, in this case 49?
please help, i have searched everywhere and no search answer answer to get this value.
$query = $wpdb->get_row("SELECT * FROM stats WHERE score = MAX(score)");
one way would be:
$query= $wpdb->get_row("SELECT * FROM stats WHERE score < max(score) ORDER BY score DESC LIMIT 1 ")
Where we order them by score and get the first result that is not the biggest score. Might need some syntax tweaks, I have only done postgreSQL for the last year or so
I'm trying to make an image database which does not keep a consistent record of ID's. For example it might go 1,2,6,7,12, but as you can see that is only 5 rows.
Inside the table I have fileid and filename. I created a PHP script to show me the image when I give the fileid. But if I give it the ID 5 which does not exist I get an error. That's fine as I want an error for that, but not for users who will browse through these images using forward and back buttons. The forward and back buttons would need to retrieve the true fileid which comes after the given ID. Hopefully that makes sense.
This is how I imagine the code to look like:
SELECT offset( WHERE fileid=4 )
That would give me the offset of the row where fileid is equal to 4. I think this is easy enough to understand. The reasons I need this are for creating the forward and back button. So I planned to add 1 or take 1 from the offset which gives me the new ID, and the new filename. That way when users browse it will skip the dead ID values automatically, but it will give an error when giving a false ID.
Going up:
SELECT * FROM table WHERE id > 'your_current_id' ORDER BY id LIMIT 1;
Going down:
SELECT * FROM table WHERE id < 'your_current_id' ORDER BY id DESC LIMIT 1;
ps: it is better to make LIMIT 2, so that you can see that you are at the first or at the last records in the database when only one record is returned.
If your results are ordered by x, ascending, the following will give you your current offeset in the table:
SELECT COUNT(*) FROM tablename WHERE x < x_of_your_current_item;
If you just want to SELECT the next or previous row, you can skip having to do two queries by just directly selecting one row:
SELECT * FROM tablename WHERE x > x_of_your_current_item ORDER BY x LIMIT 1;
will give you the next item (and similarly < and adding DESC to the order-by would give you previous).
You can use offset. Initially set offset as zero.
First time your query will be
SELECT * FROM TABLE order by table_id LIMIT 0,1
and next
SELECT * FROM TABLE order by table_id LIMIT 1,1
..
and so on
This way one will get records from the beginning till the end.
Now about back and forward buttons.
Back Button: First time or whenever offset is zero disable back button
Forward Button:
when you query for a current record you check for the next record too
i.e. after this query
SELECT * FROM TABLE order by table_id LIMIT 0,1 fire a query like this
SELECT * FROM TABLE order by table_id LIMIT current_offset+1,1 and check if the query produces any results if it produces a result then set a boolean say next = TRUE else next = FALSE;
Using this boolean enable or disable Forward button.
One more thing on click of back button send the offset as current_offset - 1 and for forward button current_offset + 1
I hope this helps. I just came across this and thought of this solution.
SELECT * FROM table WHERE ... ORDER BY id DESC LIMIT 50,10;
SELECT * FROM table WHERE ... ORDER BY id DESC LIMIT 60,10;
SELECT * FROM table WHERE ... ORDER BY id DESC LIMIT 70,10;