PHP MySQL - Start from ID x [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 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

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;

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

php sql error (from,order,where position) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
i made this:
$query1=mysql_query("select id, testo, titolo from events order by DESC WHERE id < 225 LIMIT 1");
while($query2=mysql_fetch_array($query1))
{
echo $query2['titolo'];
}
But doesn't work, i don't understand why, someone can help me?
I think the problem is in the query.
UPDATE: I made two mistakes:
The first: i have missing the column name in order by clause.
The second: The position of ORDER
Write your query as below:-
SELECT id, testo, titolo FROM events WHERE id < 225 ORDER BY id DESC LIMIT 1
As suggested by chris85, For understanding select ...from... where... order...limit,
This link is very useful.
Hope this will help you :)
You are using ORDER BY before WHERE Clause you can not get the result like this way.
Second issue is that you didn't mentioned the id column or any other column in ORDER BY.
Modified Query:
SELECT id, testo, titolo FROM events WHERE id < 225
ORDER BY id DESC
LIMIT 1
Side note:
Please use mysqli_* or PDO instead of mysql_* becuase this extension deprecated and not available in PHP 7.
you have at least 2 errors. Rewrite your query to look like:
SELECT id, testo, titolo FROM events WHERE id < 225 ORDER BY id DESC LIMIT 1
You're missing the column name in your order by clause. That section should be something like order by col_name desc. Also the where clause comes before the order by as described in above answers.

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.

Data from database of last 10 newest [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
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";

Categories