Inverse PHP mysql_fetch_array - php

Inverse PHP mysql_fetch_array
I get last 4 row from database, but I want order these 4 rows in inverse
mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT 4");
How can I do this?

mysql_query("SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 4) t ORDER BY id ASC");
i Think this should do

Related

Writing a query that returns the latest row related to a specific user

Trying to create a query that gets the most recent record related to the user. Here's my attempt
"SELECT *
FROM (
SELECT *
FROM mytable
ORDER BY id
DESC LIMIT 1)
WHERE userid = $userID";
You could also:
Select * from table
Where id =
(Select Max(Id) from table
where userid = $userID)
Simply skip the sub-query:
SELECT *
FROM mytable
WHERE userid = $userID
ORDER BY id DESC
LIMIT 1
Use Top(1)
Select TOP(1) * FROM table ORDER BY id DESC
Try:
SELECT *
FROM mytable
WHERE userid = $userID
ORDER BY id DESC
LIMIT 1

MySQL select grouping multiple rows from same table

I'm trying to create a query to select multiple rows from the same table grouping them like an array.
Now i'm selecting them using php like this:
$tks = mysqli_query($con,"SELECT * FROM hof ORDER BY tks DESC LIMIT 5");
$top_ths = mysqli_query($con,"SELECT * FROM hof ORDER BY ths DESC LIMIT 1");
$top_tha = mysqli_query($con,"SELECT * FROM hof ORDER BY tha DESC LIMIT 1");
----
I would like to merge them in a single query so i get an associative array.
Something like this:
(SELECT * FROM hol ORDER BY tks DESC LIMIT 5) AS tks
UNION
(SELECT * FROM hol ORDER BY ths DESC LIMIT 1) AS top_ths
So tks contains all the 5 rows and top_ths contains 1 row.
Is it possible ? Thanks.
to undersatnd from wich group row is, make additional field
(SELECT *, 1 as `group` FROM hol ORDER BY tks DESC LIMIT 5)
UNION
(SELECT *, 2 as `group` FROM hol ORDER BY ths DESC LIMIT 1)

How to sort sql output by division of two rows?

I want to sort the posts of my site by the result of the likes (vote) divided by the views (postviews).
Is something like this possible?
$sql = "SELECT *
from posts
WHERE id NOT IN(SELECT postid FROM postfeed)
ORDER BY vote/postviews
DESC LIMIT 1";
This is a cron job that adds the id to the another feed table, that's why I have a limit of 1.
I see what you trying to do. If you want to order by calculation then do this trick
$sql = "SELECT p.*, p.vote/p.postviews as average
FROM posts p
WHERE id NOT IN(SELECT postid FROM postfeed)
ORDER BY average DESC
LIMIT 1";
You calc value for each row and then you should create alias for it and sort by this alias.
$sql = "SELECT *
from posts
WHERE id NOT IN(SELECT postid FROM postfeed)
ORDER BY vote, postviews
LIMIT 1";
You can also add a parameter to make either of then Ascending or Descending sort like so :-
$sql = "SELECT *
from posts
WHERE id NOT IN(SELECT postid FROM postfeed)
ORDER BY vote DESC, postviews DESC
LIMIT 1";
Or
$sql = "SELECT *
from posts
WHERE id NOT IN(SELECT postid FROM postfeed)
ORDER BY vote DESC, postviews ASC
LIMIT 1";
Ah although #Robert may well have understood the question better.

How to get limit records with in Clause

I have a following query
I am getting id as a string
SELECT * from table_name
where id in (1,2,3,4)
order by created desc
limit 0,4
this Query is giving me only 4 records
i want to get 4 records from each id
Any idea ?
select * from
(
SELECT * from table_name where id = 1 order by created desc limit 4
union all
SELECT * from table_name where id = 2 order by created desc limit 4
union all
SELECT * from table_name where id = 3 order by created desc limit 4
union all
SELECT * from table_name where id = 4 order by created desc limit 4
) tmp
order by created desc

MySQL Query - Show Latest 3 Records with Order by Ascending

Is there anyway to fetch latest 3 comments with Order By id asc ?
Here is my table Structure: Table name: comments
Right Now I am using this Query:
SELECT *
FROM `comments`
ORDER BY id ASC
LIMIT 0 , 3
But it returns in result, which is obvious :
But I want to show latest 3 records , but in Ascending Order.
Like this:
Use below code:
SELECT *
FROM (SELECT *
FROM `comments` ORDER BY id DESC LIMIT 0 , 3) t
ORDER BY id ASC;
First you sort by descending id, and get 3 results, and then do ascending sort on id on these 3 results.
(SELECT * FROM `comments` ORDER BY id DESC limit 3 ) ORDER BY id ASC
Just reorder the DESC query with a second ORDER BY :)
SELECT * FROM (
SELECT *
FROM comments
ORDER BY id DESC
LIMIT 3
) t ORDER by id ASC
try this
select * from (select * from `comments` ORDER BY id desc limit 0,3) t
order by id asc;
This should do it:
SELECT *
FROM `comments`
ORDER BY id DESC
LIMIT 0 , 3

Categories