Im looking to display rolls from a MySQLi but I would like to display one yes and one no... example
If I use this code:
$sqldisplay = $mysqli->query("SELECT `id` FROM `albums` ORDER BY `id` DESC LIMIT 5");
while ($rowdisplay = $sqldisplay->fetch_assoc()) {
echo $rowdisplay['id'].'<br>';
}
It will display
10
9
8
7
6
But im looking to display it like this:
10
8
6
4
2
Is it posible? And if so, how can it be done?
Thanks for the help! :D
To display alternating rows, first double your limit.
$sqldisplay = $mysqli->query("SELECT `id` FROM `albums` ORDER BY `id` DESC LIMIT 10");
Then use a boolean switch to determine whether each row will be displayed.
$display = true;
while ($rowdisplay = $sqldisplay->fetch_assoc()) {
if ($display) echo $rowdisplay['id'].'<br>';
$display = !$display; // switch the display on/off
}
This way, you won't have to depend on the value of the ID, something that could produce unexpected results if any IDs are missing due to deleted rows, etc. A numeric ID is a surrogate key which should have no meaning other than uniquely identifying a row.
Using WHERE (id % 2) = 0 would give you only even numbers, and using WHERE (id % 2) > 0 would get you odd numbers.
You can use this along with a subquery that selects MAX(id) and returns either 1 (for odd) or 0 (for even). This will ensure that if the id is even, just even IDs will be returned, and vice-versa, when applied with the logic explained in the paragraph above.
SELECT `id`
FROM `albums`
GROUP BY `id`
HAVING `id` % 2 = (CASE WHEN (SELECT MAX(`id`) FROM `albums`) % 2 = 0 THEN 0 ELSE 1 END)
ORDER BY `id` DESC
LIMIT 5
With Paul's comment below, it's been pointed out that you can clean up the query even more, just doing WHERE id % 2 = (SELECT MAX(id) % 2 FROM albums) instead - this way you shouldn't need any GROUP BY..HAVING.
SELECT `id`
FROM `albums`
WHERE id % 2 = (SELECT MAX(id) % 2 FROM albums)
ORDER BY `id` DESC
LIMIT 5
You can also achieve this in PHP if you wish retrieve both datasets, see Don't Panic's answer for that (although I prefer to do it in MySQL if possible).
MySQL modulus % documentation
when you set id for order by.your result is According to insert each column
you must add column in you table for this .for example you can have column orderBy and you can specify the order of each display column
and then your query same
$sqldisplay = $mysqli->query("SELECT `id` FROM `albums` ORDER BY `orderBy` DESC LIMIT 5");
Simple, just check if the modulo of the ID is 0 (no remainders) to return even numbers.
If you want to return odd numbers, change the modulo to equal 1.
If you want to select odd or even numbers depending on user input, you can easily make an if statement and use Prepared Statements to input the requested number (1 for odd ID rows to be returned, 0 for even rows).
SELECT `id` FROM `albums` WHERE `id` % 2 = 0 ORDER BY `id` DESC LIMIT 5
So your code will be:
$sqldisplay = $mysqli->query("SELECT `id` FROM `albums` WHERE `id` % 2 = 0 ORDER BY `id` DESC LIMIT 5");
while ($rowdisplay = $sqldisplay->fetch_assoc())
{
echo $rowdisplay['id'].'<br>';
}
Related
I want to write a query in mysql for delete action. I want to keep first X records and for example only delete records after row number 40. I mean I want to keep first x rows and delete all rows after that.
how can I write this query?
I can delete first rows but I don't know how I can delete rows after row number X
I tried this code but this is not correct and not working.
DELETE FROM `myTable` WHERE `username` = 'name' ORDER BY `DD` DESC LIMIT 40 OFFSET 3
EDIT:
DELETE FROM `myTable` WHERE `id` in ( select `id` from `myTable` where `username` = 'name' ORDER BY `DD` DESC Limit 40, 18446744073709551615)
LIMIT 40 OFFSET 3 is for PostgreSQL.
For MySql you can try LIMIT 40,X
40 = starting at row 40
X = quantity of rows to delete after that
Obligatory Disclaimer: There are no "first rows" intrinsic to a relational database table.
However, since you've provided an ORDER BY, something like this should work (maybe):
SELECT `DD` INTO #dd40
FROM `myTable`
WHERE `username` = 'name'
ORDER BY `DD` ASC
LIMIT 39, 1
;
DELETE FROM `myTable`
WHERE `username` = 'name'
AND `DD` > #dd40
;
delete a
from sales.xxx_log_xxx as a
join (
select b.code,b.up_date
from sales.xxx_log_xxx as b
where b.code = 'code'
order by b.up_date desc limit 5,50) as aa
on a.code = aa.code
and a.up_date = aa.up_date
I have this query:
SELECT `name` FROM `products` WHERE `id` = 0 OR `id` = 4 OR `id` = 2
basically, I want the query to be sorted by the order of the OR statements (basically, the first returned object would be with ID 0, the second would be 4, and third be 2).
Is this possible?
Yes you need to use field() function in order by as
SELECT `name` FROM `products` WHERE `id` = 0 OR `id` = 4 OR `id` = 2
order by field(id,0,4,2)
Here is a demo
The table is not the same in your case but you may get an idea how it works.
Try this
select name from products WHERE id=0 OR id= 4 OR id=2 order by FIELD(id,0,4,2);
FIELD() Returns the index (position) of 0 in the 4, 2,... list. Returns 0 if 0 is not found.
Note : this will Slow down your query a bit.
I believe you are looking for the FIND_IN_SET() function:
SELECT name FROM products WHERE id IN (0,4,3) ORDER BY FIND_IN_SET(id, '0,4,3');
This is another idea -
SELECT name FROM products
WHERE id = 0 OR id = 4 OR id = 2
ORDER BY CASE WHEN id=0 THEN 1 WHEN 'id'=4 THEN 2 WHEN 'id'=2 THEN 3 ELSE 'id' END
This query giving strange result:
SELECT `user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND (`rankType` = "top5"
OR `rankType` = "top20")
ORDER BY rankType
LIMIT 0 , 30
here the SQLfiddle.
Want I am trying to achieve is:
1)To get only 5 records of top5 rank type, 20 records of rank type top20
2)I want to show the result in ascending order of rank type.(but if you see in the demo fiddle it's showing apposite, may be it is only considering 2 from 20 & 5)
(SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top5"
ORDER BY rankType
LIMIT 0, 5)
union
(SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top20"
ORDER BY rankType
LIMIT 0, 20)
If later on you want to add another set of sorting/filtering columns, wrap it all into something like
select * from ( /* previous query goes here */ ) tt
where id > 100
order by id
Note that ranktype is varchar, so it's sorted lexicographically, so top20 < top5. You'll have to employ natural sorting or some other means to get it right.
SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top5" limit 5
union
SELECT `id`,`user_id`,`rankType`
FROM `ranks`
WHERE `user_id` =23
AND `rankType` = "top20" limit 20
Your result is actually in ascending order, since column rank_type is of varchar type top20 comes first than top5 as in string comparison.
If you only want to deal between top5 and top20, a dirty solution could be:
ORDER BY rankType desc
One possibility without doing two queries and UNIONing them:
ORDER BY FIND_IN_SET(rankType,'top5,top20')
I need help on how to randomize the last 10 rows of MySql records.
$mysqld = mysql_query(select * from table where amount > amount2 and code = '$code' order by time DESC limit 1);
From the statement above I need to randomize the last 10 rows ordered by time but limited only 1 to display.
EDIT: In other words, I need to have the table ordered by time and then I need to focus on the last 10 rows. From these last 10 rows, I need to pick one and it must be random, which one I get.
Is this possible?
Thanks
Assuming that time is the time when record was inserted, this will get you the latest 10 rows from the table:
SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
Now, you can use the result as a temporary table, sort it randomly (as it's only 10 rows) and return one row:
SELECT * FROM (
SELECT * FROM `table` WHERE `amount` > `amount2` AND `code` = '$code'
ORDER BY `time` DESC LIMIT 10
) AS temptable
ORDER BY RAND()
LIMIT 1
Try....
SELECT * FROM (SELECT * FROM yerTable ORDER BY id DESC LIMIT 10) AS tmp ORDER BY RAND() LIMIT 1
Obviously replace id with any other distinct column if preferred.
I want to do this:
Check the top 10 values of points.
Here is my condition:
If there are less than 10 records (rows) found, e.g give bonus
If the points is in top ten (among hundreds records/rows), give bonus.
So, what i do is (wrong method):
SELECT points FROM `scores` WHERE id = '1' ORDER BY score DESC LIMIT 9 , 1
That will only work if i have more then 9 (at least 10) data/records.
Is there any other way?
I am thinking of using this(not very good though):
SELECT points FROM `scores` WHERE id = '1' ORDER BY score DESC LIMIT 0 , 10
Then get the last value of mysql_fetch_assoc data. Thus, how do I get the last value of mysql_fetch_assoc data?
The previous query is close to what you want:
SELECT points FROM (
SELECT points, score
FROM scores
WHERE id = '1'
ORDER BY score DESC
LIMIT 10
) AS top_ten
ORDER BY score ASC
LIMIT 1
If you want to stick with mysql_fetch_assoc getting the last value you can utilize mysql_data_seek.
Something like this:
<?php
// ... $result is the result set from your query
$result_count = mysql_num_rows($result);
if ($result_count > 0)
{
mysql_data_seek($result, $result_count - 1);
}
$row = mysql_fetch_assoc($result);
?>
try to use this:SELECT points FROM (SELECT points FROM scores WHERE id = '1' ORDER BY score DESC LIMIT 10) ORDER BY score LIMIT 1