MySQL show only the last 8 rows [duplicate] - php

This question already has an answer here:
MySQL LIMIT/OFFSET: get all records except the first X
(1 answer)
Closed 9 years ago.
On my index page I have the "latest work" which shows 8 portfolio pieces
and I need it to only grab the ones that have active = 1 as well
and I have something like this:
$sql = 'SELECT * FROM portfolio WHERE active = 1';
I tried doing this but its not working and i get errors when I try to pass it in PHPMyAdmin as well.
$sql = 'SELECT * FROM portfolio
WHERE active = 1
WHERE [id] > SELECT
MAX([id]) - 8 FROM portfolio';
Any ideas?

If you want to get the most recent 8 active pieces, use LIMIT as well as ORDER BY
$sql = 'SELECT * FROM portfolio WHERE active=1 ORDER BY id DESC LIMIT 8'

SELECT * FROM portfolio WHERE active=1 ORDER BY id DESC LIMIT 8

$sql = 'SELECT * FROM `portfolio` WHERE `active` = 1 ORDER BY `id` DESC LIMIT 8';
ORDER BY id DESC -> orders rows by highest to lowest value of ID, use ASC for opposite.
LIMIT 8 -> only 8 first rows

Related

How to write a query that selects 9 rows randomly from database?

SELECT * FROM tbl_specialisation
WHERE LENGTH(spec_specialise) < 20 ORDER BY spec_specialise LIMIT 9
add to this query
"SELECT * FROM tbl_specialisationWHERE LENGTH(spec_specialise) < 20 ORDER BY RAND() LIMIT 9 ";
USE ORDER BY RAND()
$sql= "SELECT * FROM `tbl_specialisation`WHERE LENGTH(spec_specialise) < 20 ORDER BY RAND() LIMIT 9 ";
Your question is not clear because you are asking 9 random fields but in SQL query you are trying to get 9 records. If you want to get 9 records from table then use following.
$sql= "SELECT * FROM `tbl_specialisation`WHERE LENGTH(spec_specialise) < 20 ORDER BY RAND() LIMIT 9 ";

Pulling specific data on top and restricting its limit in MySQL 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

Select Next mysql row without refer to id

This is My Table:
#id# #cpc#
100 10
87 9
101 9
4 6
188 5
it's sorted DESC according to 'cpc' column.
I Want to extract the rows one by one without referring to id.. such as you can see it.
SELECT * FROM table ORDER BY cpc DESC
first result is with the id 100
next one is with id 101 and cpc 9 not 87.. as the id is only increasing.. so it selects wrong rows not as what i want.
You can do something like this in php:
$sql = mysql_query("SELECT * FROM yourTable ORDER BY cpc DESC");
while(($row = mysql_fetch_array($sql)){
$id = $row['id'];
$cpc = $row['cpc'];
Select them using limit and offset:
SELECT *
FROM table
ORDER BY cpc DESC
LIMIT 1 OFFSET 0;
Then:
LIMIT 1 OFFSET 1
LIMIT 1 OFFSET 2
and so on.
You need to edit your ordering.
SELECT *
FROM table
ORDER BY cpc DESC,
Id ASC

Retrieving database info while requesting other data

I am not sure how to fix this issues.
Basicly, I need to be enable to retrieve data from a database.
But this needs to be limited to the users account plan.
For example: an user has a membership plan, which comes with 5 emails.
He will need to be enable to see 5 emails, instead of my whole email database getting extracted.
Code:
$SQLSelect = $odb -> query("SELECT * FROM `accounts` ORDER BY `ID` DESC LIMIT = '" . ($_POST['email']) . "'");
Desc limit is option what allows to display limited amount.
Example:
You have table with 5 rows and those rows have auto increment id.
wtih select * from users order by id desc limit 3, you will select users with id 1 , 2 , 3 because at begining you order it by id, so it select 1 ,2 ,3 ,4 ,5 and then u say that you need only 3 of them using desc limit. So it seems like you got desc limit wrong...
If i got your example then you need something like this :
$limit = 5;
query("SELECT * FROM `accounts` ORDER BY `ID` DESC LIMIT = ".$limit)
So here user will only see accounts with id 1 , 2 , 3 , 4 and 5.
If i didn't say what you need, i rly didn't get your question

How to get data from 2 tables? [duplicate]

This question already has answers here:
Querying 2 Tables in a single query
(3 answers)
Closed 8 years ago.
I am trying to create a website with an existing database.
The database has two tables, one of them contain the post info like title, id, content, etc etc,
the other table contains the post ID and the post category.
For example: I want to take a post from a specific category.
$sql = 'SELECT * FROM posts WHERE status = "publish" ORDER BY date DESC LIMIT 10';
This is a simple function, can some one help me please how to modify it, to get the category from the other table and the post info from the other table.
SELECT * FROM posts, posts_category WHERE category = 'category'
AND
status = 'publish'
AND
posts_category.post_id = posts.id
ORDER BY date DESC LIMIT 10;
$sql = "SELECT * FROM posts WHERE status = 'publish' AND pid IN (SELECT pid FROM pcategory WHERE ptype='category_name') ORDER BY date DESC LIMIT 10";

Categories