/    Sign up×
Community /Pin to ProfileBookmark

Products price
T-shirt 58
Sport Shoes 39
Handphone 99
Hand Bag 64
Watch 99
Story Book 40
Short pants 89

Unacceptable Answers:
1. SELECT Products, MAX(price) FROM Stock

  • 2.

    SELECT Products, MAX(price) AS prices FROM Stock

  • 3.

    SELECT Products, price FROM Stock WHERE price = 99

  • how can i do the query to get the highest price, and products from table Stock without using MAX? any idea?

    to post a comment
    PHP

    8 Comments(s)

    Copy linkTweet thisAlerts:
    @ShrineDesignsMay 26.2004 — SELECT <span><code>Products</code></span> FROM <span><code>Stock</code></span> ORDER BY <span><code>price</code></span> DESC
    Copy linkTweet thisAlerts:
    @ltingauthorMay 26.2004 — can't this will list out all the results ...


    i only want the highest price ...


    e.g. handphone and watch both are 99

    so the results will show

    handphone 99

    watch 99

    SELECT Products FROM Stock ORDER BY price DESC limit 2 is not allowed also ... cos this work only when i know there are 2 highest price items in the table ...
    Copy linkTweet thisAlerts:
    @ConorMay 26.2004 — Why cant you use max? I know its possible with max I believe, the only other wat [B]I[/B] can think of is doing 2 querys.
    Copy linkTweet thisAlerts:
    @ShrineDesignsMay 27.2004 — SELECT <span><code>Products</code></span> FROM <span><code>Stock</code></span> ORDER BY <span><code>price</code></span> DESC LIMIT 1
    Copy linkTweet thisAlerts:
    @jakesterMay 27.2004 — Try

    SELECT Products, Prices FROM Stock ORDER BY price DESC Limit 1
    Copy linkTweet thisAlerts:
    @ltingauthorMay 28.2004 — cna't use limit 1 ..

    cos there are two records with highest price .. if limit 1 .. then it will only show one highest price record ...
    Copy linkTweet thisAlerts:
    @ConorMay 28.2004 — the only way I can think if is with two queries

    [code=php]
    $query="SELECT Prices FROM Stock ORDER BY price DESC LIMIT 1";
    $result=mysql_query($query);
    $row=mysql_fetch_array($result);
    $price=$row['price'];
    $query2="SELECT PRICES FROM STOCK WHERE price='$price'";
    $result2=mysql_query($query2);
    [/code]


    but yeah theres probably a much more efficient way.
    Copy linkTweet thisAlerts:
    @dr_johnJun 01.2004 — select products, price from stock

    where price =(select max(price) from stock)


    This is a query / subquery problem. You need to know the max(price) first, (the sub query), then use that to select products that match it (the outer, or main query)

    This will return ALL products where the price = the max(price)
    ×

    Success!

    Help @lting spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...