Limiting top rows in - php

I'm new to Stackoverflow, and I'm pretty new to MSSQL too (I've been using MYSQL for a couple of years now), and I was wondering if anyone could please help me with a query I'm having trouble with?
I've made a story archive page for a site that I'm putting together, and it's working fine, but the only problem is that I want to archive everything except for the top 6 stories in my table.
I came across a piece of code which will ignore the top few results (6 in my case), but I'm a little bit stumped as to how to incorporate it into my query.
I've tried a few things but I keep getting error messages - I think I'm way off track.
Any help that anyone can give me would be hugely appreciated!
Thank you very much in advance:)
This is the query I found:
SELECT *
FROM PageContent
WHERE id NOT IN (
SELECT TOP 6 id
FROM PageContent
ORDER BY date)
This is my query:
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER by PageId desc) AS RowNum
FROM PageContent WHERE pagestory_type = 'latest_news'
) AS MyDerivedTable
WHERE MyDerivedTable.RowNum BETWEEN $qpage_srt and $qpage_fin ORDER by date desc";

If you have SQL Server 2012 you can try:
SELECT *
FROM PageContent
ORDER BY [date]
OFFSET 6 ROWS

Why can't you just use the TOP clause with a select from the table ordered by the column you need? Similar to the sample you found.
SELECT TOP X * -- add fields as needed
FROM PageContent
WHERE pagestory_type = 'latest_news'
ORDER BY PageId DESC

i dont find any difference in your query except the parameter variables
$qpage_srt and $qpage_fin
it should be #qpage_srt and #qpage_fin in Ms sql...

Related

Beginner MySQL issues with invalid use of group function

I've been googling around and can't seem to find how to fix this. I'm currently using MySQL and am trying to run this
SELECT song_id FROM ‘played_songs’ WHERE id=MAX(id)
When I run this, I get the error message
1111 - Invalid use of group function
Does anyone know what I am doing wrong here? For reference, I'm trying to get the latest row's song_id in a pre-existing database. The ID increments so the latest value is the max ID in the column. When I get that column, I would like to get it's song_id in that same row.
Sorry if this is a super basic question, I've never done this before.
Thanks!
you can't use max in where clause, try this:
SELECT song_id FROM played_songs WHERE id=(SELECT max(id) FROM played_songs)
but better solution is:
SELECT song_id FROM played_songs ORDER BY id DESC LIMIT 1;
You need to use a "Having" instead of a "where". A Having is used when you are operating on a previous calculation (getting max id is a calculation mysql has to make). I would write like this.
SELECT song_id, MAX(id) as
max_id FROM ‘played_songs’
HAVING max_id=id

Complex SQL Query - Popular Content

So, I'm writing a query to select data from the database, and display it in an order of popularity. I understand that this is simple enough to do if it were just ordering it through something such as ORDER BY numLikes DESC or something, but in this case, I am wanting to order the content by numLikes and through datePosted.
How would I sort the content in a way that it displays the top content from today's date? If it sorted purely through numLikes, The top content would float to the top and stay there. I want this content to be sorted through a daily basis. This is to allow for a system where the user can choose popular posts from the past day, past week, past month, and all time top posts.
Could this be done through a single SQL query? Would an SQL multi-query have to be done? Is SQL powerful enough to do this, or is PHP required to play a role?
Thanks!
You just need to sort on datePosted first and then by numLikes. Something on the following lines:
SELECT * FROM tableName ORDER BY datePosted, numLikes DESC
If you want top post between a particular range, you can do:
SELECT TOP 1 * FROM tableName
WHERE datePosted BETWEEN [MinDate] AND [MaxDate]
ORDER BY datePosted, numLikes DESC
Replace [MinDate] and [MaxDate] with the dates you need, e.g. for the past week, they would be something like April-14 and April-20.

PHP MySQL select * WHERE id = > 100

I have a blog with English and Swedish posts. I first wrote in English and then switched to Swedish.
My question is, If there is any way to only display post with a higher id?
Like:
select * FROM blog WHERE id is bigger than 100
Anyone get my drift and know if this is possible? :)
I don't want to delete the old posts, and I also don't want people to see them.
Thank you!
I think you mean
Select *
From blog
Where Id > 100
Order by ID DESC
See http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html for more information.
If the ID is auto-incremented, this will bring the newest articles first, and not display the one with ID smaller than 100.
Simple Try this
SELECT * FROM blog WHERE id > 100
Even though all the answers seem correct, it looks to me you actually want to have just the last 20 or so blog posts. In that case use LIMIT.
select * from blog order by id desc limit 20
If you are working with a system for showing posts, you might rather want to show the latest few posts, which can be done with a query like this:
SELECT * FROM `blog` WHERE ORDER BY `id` DESC LIMIT '100';
MySQL Select query have syntax for bigger then is > you can do
select * FROM blog WHERE id > 100
Hope above will help.

Pagination query(SQL) in AS400/DB2

I have been working on try to create php web-based paging for our tables
which have over a million rows.
Based on what I have read, I have 3 options
retrieve all rows in resultset - not possiblefor me coz of the size
retrieve 1000 rows, store in temp table and create an iterator for
it and page through it - too many queries - too many inserts!!
run a query each time if someone opts page forward or backwards
Right now I am trying to get option 3 working.
I have the first page showing up as
"select * from accout order by acct fetch first 10 rows only"
Page next
"select * from account where acct>(last record) order by acct fetch
first 10 only"
page last record
"select * from account where acct=(select max(acct) from account)"
The problem is showing the previous page and i really would appreciate
help in this.
SELECT *
FROM (
SELECT
*,
ROW_NUMBER() OVER (ORDER BY acct) AS RowNum
FROM
account
) AS Data
WHERE
RowNum BETWEEN 100 AND 110;
First, you should get rid of the SELECT *. Select only the fields you need.
Place an index on acct, this will help the ROW_NUMBER() OVER (ORDER BY acct) construct.
Use a SELECT COUNT(*) FROM account to determine how many pages you will have.
Also read Fastest most/efficient way to do pagination with SQL searching DB2
The LIMIT..OFFSET solution is supported in DB2 10+.. For older versions, you have to enable the MySQL compatibility with:
$ db2set DB2_COMPATIBILITY_VECTOR=MYS
$ db2stop
$ db2start
in db2cmd in order to use that syntax.
SELECT * FROM foo LIMIT 10, 1;
try the limit and offset in mysql..
this mostly use in creating pagination

MySQL & PHP - Select 6 rows, each with a different 'WHERE' clause

I'm pretty good with MySQL, but this is something I have never done. What I want to do is make an SQL code to select 6 rows, each with their own WHERE clause.
What I am trying to do is get 6 rows, and each will be the most recent "video" that was posted. there are 6 categories, so that's why I have 6 rows. I want it to pull the most recent by it 'id' number.
I'd do it with 6 different SQL queries, but I assume that would be slower (unless this is the only way to do this?)
From that small snippet, I would like to end up with is this:
2 --> 21
6 --> 16
8 --> 14 (Picks 14 since it's largest.)
Final Working Code
$sql="SELECT video_category, MAX(video_id) AS video_id FROM videos GROUP BY video_category";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)) {
echo $rows['video_category'] . " --> " . $rows['video_id'] . "<br>";
}
something like
select distinct category, video_id from table_name order by id DESC
If you have 6 categories in the db, you would get 6 rows, all having highest id in their category
Please share your table structure. Nevertheless, i think the following query should do the trick:
SELECT category_id, MAX(movie_id) most_recent_movie_for_category FROM movies GROUP BY category_id
Thanks for posting the table structure. This is just a simple GROUP BY with a MAX aggregate on video_id.
SELECT video_category, MAX(video_id) AS video_id FROM videos GROUP BY video_category;
You have two options:
Determine common WHERE clause that will result in what you need.
(probably preferred one) Make some query involving UNION (SELECT ... FROM ... WHERE ... UNION SELECT ... FROM ... WHERE ... etc.)
Let me know if you have any questions. I believe without your database structure it would be hard to help you more.

Categories