Data from database of last 10 newest [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I just wanna show last 10 or 30 depends on list if there is big data, searched over the net but all by correcting I can see only the 1st data not newest one.
$sql = "SELECT id, tag, count FROM url ORDER BY id, tag, count ASC LIMIT 1";
I am testing with two datat on database and want to display last one but all the time it shows the oldest one (first data), I changed ASC and DESC also, both time result is same.

Try this..
Order by id using "DESC" it get last to first and limit 0,10 means it display last 10 record
$sql = "SELECT id, tag, count FROM url ORDER BY id DESC LIMIT 0 10"

Use this one:
$Query = "SELECT id, tag, count FROM url ORDER BY id DESC LIMIT 10"

Change your query & try this:
$Query = "SELECT id, tag, count FROM url ORDER BY id DESC LIMIT 10";

Related

Mysql: How do I order groups without losing individuals rows within groups [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am on phpmyadmin, and I have data:
query url score
a www.google.com 3
a www.facebook.com 2
a www.google.com 1
I want to 'group' entries by their domain, and then order (desc) the domain groups by each group's highest score (some in the comments have found this wording clearer: 'order desc by each group's highest score') so I get:
query url score
a www.google.com 3
a www.google.com 1
a www.facebook.com 2
Trying: select * from table order by score desc, url asc doesnt work. It gives (no apparent change):
query url score
a www.google.com 3
a www.facebook.com 2
a www.google.com 1
I'm apparently not communicating my troubles clearly. If you can see where I can make it clearer, let me know.
For reference, I have re-posed the question here and it has an accepted answer: How do I order groups by each group's highest value
According to your expected output, you don't need grouping at all, just a multiple column order by clause:
SELECT *
FROM mytable
ORDER BY var1 ASC, var2 DESC
If i understand, you want order by var1 first as asc and then by var2 as desc.
What about this:
SELECT * FROM table ORDER BY var1 ASC, var2 DESC;

Use result from sql in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have following in php code:
$result=mysql_query ("SELECT title, image FROM board order by date limit 15");cq();
I would like to replace 15 with result from sql query:
SELECT count FROM cat_count
So I put to php but it doesn't work:
$cat_count=mysql_query ("SELECT count FROM cat_count");cq();
$result=mysql_query ("SELECT title, image FROM board order by date limit ".$cat_count."");cq();
Thanks
You can AS alias to get count in a variable and then use it in your next query.
$result=mysql_query("SELECT count(*) as total from cat_count");
$data=mysql_fetch_assoc($result);
echo $data['total'];
Then use it as:
$result=mysql_query ("SELECT title, image FROM board order by date limit ".$data['total']);cq();

how to get a unique record from select query in mysql each time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to fetch random record from the table but repeated game id record is will become out only once at a time. I am sending a screenshot of my table
You need to use MySQL RAND() function.
Query:
SELECT * FROM lessons_game ORDER BY RAND() LIMIT 1
// Put proper fields instead of *
Explanation:
ORDER BY RAND() will shuffle records random records from DB Table.
And we will get random records after we execute the query result set.
We are putting LIMIT 1 so only one record will be pulled out.
Try the following command
SELECT * FROM lessons_game ORDER BY RAND() LIMIT 1

Return the latest 10 records from a mysql query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Here's the link what I mean -
i.stack.imgur.com/yYQbu.png.
Query from 2 columns name and version.(may only with one column I'll put version in name column).
So , Wheres matches name from title or post from same column name and returns all lowest number from version(column).
Can anyone give me example of some query?
So far I have this query:
$name = substr($row[2],0,10);
$q = mysql_query("SELECT name from films WHERE name LIKE '$name'") or die(mysql_error());
I'm assuming your table has an id column as the primary key with auto increment.
In which case, you need to add an ORDER BY and LIMIT to your query.
$q = mysql_query("SELECT name from films WHERE name LIKE '$name' ORDER BY id DESC LIMIT 10") or die(mysql_error());
ORDER BY id DESC will make your query put the latest records first.
LIMIT 10 will ensure that you only grab 10 records.

PHP MySQL - Start from ID x [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm making a newssystem, the last ID (so the last news) must be at the top, and now I want that below the first news box (not in the same box as the last news) are coming the other news, so that the last ID is not included. I've no ideas.
Last news code:
<?php $last_news = mysql_query("SELECT FROM news ORDER BY id DESC LIMIT 1"); ?>
Other news, below the last news: (?)
<?php $other_news = mysql_query("SELECT FROM news ...?"); ?>
Try to use limitwith the offset 2
<?php $other_news = mysql_query("SELECT FROM news order by id desc limit 2,numerOfNewsYouWillShow"); ?>
This will not show you the last news. but the news before. Hope I have understand your issue correct.
Please do not use mysql_* functions. The mysql_ API is deprecated.
Another way to do this is in stead of retrieving the data from the database in 2 queries is to retrieve the data once and then split it in php.
$last_news = array_shift($allnews);
Then $allnews will contain all the other news and $last_news the latest news article. This will also mean you only hit your db once which is better for scaling.
Please do not used the mysql_* functions. They are depreciated because of security issues. Use PDO or MYSQLi in stead
More info on shift function: http://php.net/manual/en/function.array-shift.php
below the last news, you can use
ORDER BY id DESC LIMIT 1,1
id field
23 last news //ORDER BY id DESC LIMIT 1
19 yesterday //ORDER BY id DESC LIMIT 1,1
18 old
for one query
order by id desc limit 2

Categories