Pulling specific data on top and restricting its limit in MySQL PHP - php

I have following sql query which pulls data with the id 627 to top and then it displays data with other ids
$sql_query = "select * from listing ORDER BY case when listing.makaan_id='627' then 1 else 2 end, listing_id DESC LIMIT {$start}, {$limit}";
im using php pagination system to paginate the results where i set the pagination limit to 10. Now i want to show only 4 results on top with the id 627 and rest with other ids, so in a set of 10 results 4 would be from the id 627 and other 6 will be from other ids, do i have any solution for this without writing another separate sql statement?

You can try something like this
SELECT *
FROM listing
WHERE makaan_id = 627
ORDER BY listing_id DESC
LIMIT 4
UNION ALL
SELECT *
FROM listing
WHERE makaan_id <> 627
OR makaan_id IS NULL
ORDER BY listing_id DESC
LIMIT {$start} - 4, {$limit} - 4

Related

WooCommerce-mysql - get list of products in a category

I want to retrieve all products in side a subcategory . this is the code I've :
SELECT * from `wp_term_relationships` where term_taxonomy_id=$subcatId and
object_id in(select ID from `wp_posts` where `post_type`='product' and post_status='publish' and ID=wp_term_relationships.object_id)
the problem is , this code return about 20 products but when I go to that category in website ,it returns about 40 products.
could you help me ? I need a code to return a list of products inside a category .
SELECT * from `wp_term_relationships` where term_taxonomy_id=$subcatId and object_id in(select ID from `wp_posts` where `post_type`='product' and post_status='publish' and ID=wp_term_relationships.object_id) LIMIT 0,15000000
Use Limit keyword in your mysql query.
Limit accepts start and end value.
if you are giving Limit 5 it will display only top 5 records.
If you are giving Limit 5,10 it will display records between 5-10.
If you are giving Limit 0,big number (eg. Limit 0,100000000) it will display all the records upto 100000000.
Select all records using MySQL LIMIT and OFFSET query

Pagination query using SQL

I have a table called 'items'
It has the following columns: id, name, stock, price, and category_id.
My query is
SELECT * FROM items WHERE category_id = {$category_id} LIMIT 10;
my logic for next page is store all ids in a variable called $oldIds and the next query is:
SELECT * FROM items WHERE category_id = {$category_id} AND id NOT IN ($oldIds) LIMIT 10
for another page is i store again the ids from 1st page and 2nd page do again the query
SELECT * FROM items WHERE category_id = {$category_id} AND id NOT IN ($oldIds) LIMIT 10
Do i continue using this style of query or is there a better way of querying?
for mysql
First statement should be:
mysql_query("SELECT * FROM {$statement} ORDER BY datetime ASC LIMIT {$limit} OFFSET {offset}
if 10 records per page is what you are looking for and you want to show records on page 2 i.e. records 11-20 your query will look like:
mysql_query("SELECT * FROM {$statement} ORDER BY datetime ASC LIMIT 10 OFFSET 10

Retrieve database entries starting from a particular row

Say I have 50 rows in my database table and I wanted to start retrieving database entries starting with row 9.
How can I retrieve data from a database table starting at row 9?
My code for pulling data from my db table:
$sql = mysql_query("SELECT * FROM listheadlines ORDER BY id DESC LIMIT 20");
You can use OFFSET keyword like this:
$sql = mysql_query("SELECT * FROM listheadlines ORDER BY id DESC LIMIT 20 OFFSET 9");
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter ( mysql.com ).
"SELECT * FROM listheadlines ORDER BY id DESC LIMIT 9, 18446744073709551615"
Or to get only 20 rows starting at offset 9:
"SELECT * FROM listheadlines ORDER BY id DESC LIMIT 9, 20"
Try this
mysql_query("SELECT * FROM listheadlines ORDER BY id Desc").setFirstResult(9);
Please check this once
How to fetch rows from middle of the table in mysql using hibernate?

Selecting data from the result of an ORDER BY query

Let's say I have this sql query:
SELECT * FROM questions ORDER BY votes LIMIT 3
That is loaded on one page. How do I get the the next 3 by providing the ID of the last one retrieved(1). Example of a table:
ID votes
--------
4 29
45 26
1 23
7 13
23 5
9 2
SELECT * FROM questions ORDER BY votes LIMIT 3, 3
You can select range using limit. Ex Limit x,y where x=start, y=length
EDIT:
Since you saying that you want to get this by passing the id. You can obtain relevant data set using following kind of query.
SELECT * FROM questions WHERE votes <= (SELECT votes FROM questions WHERE id = 1) AND id != 1 ORDER BY votes DESC LIMIT 3
However there is an issue if you have multiple records with same votes. So I would like to suggest you keep the record count you have displaced so far.
$displayed = 0;
$rowsPerPage = 3;
So you can simply get data by passing these parameters without an issue.
$start = 1;
if($displayed >0)
$start = $displayed + $rowsPerPage;
$sql = "SELECT * FROM questions ORDER BY votes LIMIT $start, $rowsPerPage ";
In next page load or pagination increase $displayed.
$displayed += $rowsPerPage;
You have to keep $displayed as a Session if you have page loads, Otherwise it could be a parameter.
You can use offset SELECT * FROM table_name LIMIT offset,limit.
Example:
SELECT * FROM questions ORDER BY votes LIMIT 3,3
You should use LIMIT <offset>, <count> tag instead of the id of the last on retrieved, since you are ordering by votes, not by id.
SELECT * FROM questions ORDER BY votes LIMIT 3, 3
Use below
Set 1 : SELECT * FROM questions ORDER BY votes LIMIT 3
Set 2 : SELECT * FROM questions ORDER BY votes LIMIT 4, 3
Set 3 : SELECT * FROM questions ORDER BY votes LIMIT 7, 3
and so on...
You need to keep on changing the offset value...
Good Luck!!!

PHP - Limit SQL results and show them by id

How can you LIMIT SQL results and show them by order id once you refresh the page? For en example:
Refresh #1;
Show Query with id 20
Rrefresh #2
Show query with id 21
Rrefresh #3
Show query with id 23
etc etc.
You can store the last showed id in a Session and than increment it on the next show.
to limit result you can use LIMIT:
SELECT * FROM `table` LIMIT 5
to order the result use ORDER BY:
SELECT * FROM `table` ORDER BY `id` ASC
Did you mean - how to show latest N items ordered by id in descendant order?
select *
from table
order by id desc
limit 0, 10
(N = 10 in this case)

Categories