473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Mysql Query Question

Hi,

I have a database that stores in a field the number of views for a product.

I want to write a query that gets the two highest views, so I can then
output the result.

I have this so far, but not sure how to adapt it to get the two highest
views.

mysql_query("SELECT max(views) FROM stock WHERE status='enabled' AND photo1
!= ''");

I then plan to loop through the results and echo the photo value for each
result.

I am sure this is easy, but just can't figure out the correct syntax.

Thanks,

YoBro
Jul 17 '05 #1
4 2417
I noticed that Message-ID: <AO****************@news.xtra.co.nz> from
YoBro contained the following:
I have this so far, but not sure how to adapt it to get the two highest
views.

mysql_query("SELECT max(views) FROM stock WHERE status='enabled' AND photo1
!= ''");


order it by views and simply print the top two records
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2
YoBro wrote:
Hi,

I have a database that stores in a field the number of views for a product.

I want to write a query that gets the two highest views, so I can then
output the result.

I have this so far, but not sure how to adapt it to get the two highest
views.

mysql_query("SELECT max(views) FROM stock WHERE status='enabled' AND photo1
!= ''");

I then plan to loop through the results and echo the photo value for each
result.

I am sure this is easy, but just can't figure out the correct syntax.

Thanks,

YoBro

mysql_query("
SELECT MAX(views) AS view_count
FROM stock
WHERE status = 'enabled'
AND photo1 IS NOT NULL
ORDER BY view_count DESC
LIMIT 0,2
");

I think this is correct but I haven't tested it.

Michael.
Jul 17 '05 #3
"YoBro" <yo***@wazzup.co.nz> wrote in message
news:AO****************@news.xtra.co.nz...
Hi,

I have a database that stores in a field the number of views for a product.
I want to write a query that gets the two highest views, so I can then
output the result.

I have this so far, but not sure how to adapt it to get the two highest
views.

mysql_query("SELECT max(views) FROM stock WHERE status='enabled' AND photo1 != ''");


Micheal was very close:
mysql_query("
SELECT *
FROM stock
WHERE status = 'enabled'
AND photo1 IS NOT NULL
ORDER BY views DESC
LIMIT 2
");

You don't want to use the max function because that would cause you to
receive only one row... containing just the highest view count and no other
data. The querey above will return the entire row for each of the two
records having the two highest values in views. There is no guarantee what
will happen if more than one two rows match the highest value or if two or
more rows match the second highest value. You might want to have additional
fields specified in the Order By clause to control what happens then.
Perhaps a last viewed date so that the most recently viewed of the most
viewed bubbles to the top.

- Virgil

Jul 17 '05 #4
Hi,

Thanks for great replies.
And it was so simple, I should have thought have that.

Virgil, thanks for the advice, I will have to add a date last viewed field,
because initially all the stock in the database will be 0. So many are the
same.

Cheers,

YoBro

"Virgil Green" <vj*@obsydian.com> wrote in message
news:q5*******************@newssvr22.news.prodigy. com...
"YoBro" <yo***@wazzup.co.nz> wrote in message
news:AO****************@news.xtra.co.nz...
Hi,

I have a database that stores in a field the number of views for a product.

I want to write a query that gets the two highest views, so I can then
output the result.

I have this so far, but not sure how to adapt it to get the two highest
views.

mysql_query("SELECT max(views) FROM stock WHERE status='enabled' AND

photo1
!= ''");


Micheal was very close:
mysql_query("
SELECT *
FROM stock
WHERE status = 'enabled'
AND photo1 IS NOT NULL
ORDER BY views DESC
LIMIT 2
");

You don't want to use the max function because that would cause you to
receive only one row... containing just the highest view count and no

other data. The querey above will return the entire row for each of the two
records having the two highest values in views. There is no guarantee what
will happen if more than one two rows match the highest value or if two or
more rows match the second highest value. You might want to have additional fields specified in the Order By clause to control what happens then.
Perhaps a last viewed date so that the most recently viewed of the most
viewed bubbles to the top.

- Virgil

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bruce A. Julseth | last post by:
Newbie Question: Is there a way to import MS Access into MySQL? Maybe create a CSV or something. If so, what would be the SQL to do this? Thanks... Bruce
2
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
0
by: Russ | last post by:
Hi, I'm doing some web development and have mysql (3.23.x) installed to test with (win2k, but I don't think this question is particularly platform specific). I have a reasonably complex query...
51
by: w_curtis | last post by:
I'm an Access user, and I'm trying to learn MySQL and then PHP so I can make some web databases. But it just isn't clicking. I've followed some tutorials, and picked up a book, but just getting...
1
by: Cern | last post by:
Is it somebody out there who has made a migration from an Oracle server to an MySQL server?? The scenario is as simply: I've got a Oracle 8 server with a database with content that I want to...
0
by: Murphy | last post by:
I am currently migrating a db from SQL Server & MySQL and ? (front end yet to be decided upon) As I familiarise myself with MySQL populating tables etc can be quite clumbersome. After reading the...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
8
by: Garry | last post by:
How do I cycle through a MySQL query result one row at a time so that I can do some work on each individual row, instead of having the whole query scroll by. I need to have the ability to post...
10
by: Caffeneide | last post by:
I'm using a php script which performs three xml queries to other three servers to retrieve a set of ids and after I do a query to mysql of the kind SELECT * FROM table WHERE id IN ('set of ids');...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.