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 ";
Related
I'm just starting with MySQL so I would like to know how can I select only 5 random rows in the last 50 entries of my database? I hope you understand my question.
I'm using PDO and what I have now is this:
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
Then I use a PHP foreach loop...
Thank you
The challenge is determining the last 50 entries. Assuming you have an auto-incremented id, you can do:
SELECT a.*
FROM (SELECT a.*
FROM articulos a
WHERE cat = '$ArtCat'
ORDER BY id DESC
LIMIT 50
) a
ORDER BY RAND()
LIMIT 5;
The key idea is the subquery to get the last 50 entries, and then the final query to get the 5 random rows. The subquery needs to specify how you identify the last 50.
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() LIMIT 5 ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
just add limit
i assume in your table you have some date column so we can get last 50
SELECT * FROM (SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY created_tiem desc limit 50 ) t order by RAND() limit 5;
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
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
I have a table containing rows with timestamped.
Normally if I want to get the latest 20 rows out according to the time. I use:
$sql = "SELECT *
FROM comment
ORDER BY time DESC
LIMIT 20";
But now, I want to get the latest comments AFTER the latest 20 rows and LIMIT to 10. That means rows 21-30.(of course , everything is according to timestamp)
How can I do that using MySQL?
MySQL has a built-in offset that you can use with LIMIT:
$sql = "SELECT * FROM comment ORDER BY time DESC LIMIT 10, 20";
Also, refer to this SO post: MySQL LIMIT/OFFSET: get all records except the first X
$sql = "SELECT * FROM comment ORDER BY time DESC LIMIT 20, 10";
Hope it will select from 21 to 30 records
sql = "SELECT * FROM comment ORDER BY ID DESC LIMIT 20, 10";
Try a mixture of limits
$sql = "select * from (SELECT * FROM comment ORDER BY time DESC LIMIT 30) as A order by time ASC limit 10";
The mysql built in offset method others have posted looks better though.
There are two options:
Get 30 rows and use PHP to split the result set into a group of 20
and a group of 10.
Send two queries, one for 20 and one for 10 rows.
My code get the last 10 values from a table. This table has te structure id,text and by the moment it has 20 rows. I use this piece of code
<?php
$query = mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT 10");
// Fetching data stuff here
?>
and it returns the data for IDs 11-20. (The last 10 of the 20)
I want to get the previous 10 values from this 1-10 via AJAX. I thought maybe this will work
$previous_id= $_GET["last"]; // This time it will be 11
mysql_query("SELECT * FROM `table` ORDER BY `id` DESC LIMIT $previous_id, -10");
But I'm doesn't, any suggestion?
Thanks
PS: This is not a gimme teh codez question, I just want to know how to make that query work.
If I understand you correctly you want to get the 10 rows before the row with id = $previous_id, ordered by descending ID.
If that's the case your best bet would be to use a WHERE condition. As you are ordering by id DESC you want the first 10 rows with id > $previous_id:
SELECT * FROM `table` WHERE `id` > $previous_id ORDER BY `id` DESC LIMIT 10
These will be the 10 rows before $previous_id in your original query.
Simply use positive number instead of -10:
"SELECT * FROM `table` ORDER BY `id` DESC LIMIT $previous_id, 10"
Let's suppose $previous_id is 5, the query would be:
"SELECT * FROM `table` ORDER BY `id` DESC LIMIT 5, 10"
The returned rows will be starting from 5 and 10 records.