Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: MySQL Queries

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default MySQL Queries

    I am attempting to figure out how to write MySQL queries for two scenarios.

    Given example data similar to this:

    Name Val1 Val2 Val3
    AAA 10 70 30
    BBB 30 60 90
    CCC 20 40 30


    Scenario 1:
    Get the top 3 rows by a combination of Val2 and Val3 (repeating is possible).
    Expected Example Output:
    BBB - 90
    AAA - 70
    BBB - 60

    Scenario 2:
    Get the top 3 rows by a combination of Val2 divided by Val1 and Val3 divided by Val1 (repeating possible).
    Expected Example Output:
    AAA - 7
    AAA - 3
    BBB - 3


    Does anyone know how I would create a MySQL query to do that sort of thing? I know how to get ordered data based on one column, but not multiple columns, or by mathematics on multiple columns.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: MySQL Queries

    Try a UNION to combine two queries. For instance, in scenario 1, UNION two selects, the first gets Val1 and second Val2, then query this and order as appropriate. In scenario 2, have 2 subqueries perform the selects and math operations, UNION them, then select from and order as appropriate.
    An example of the UNION
    SELECT Name, Val1 as value FROM mytable UNION ALL select Name, Val2 AS value FROM mytable

    You could perform the division as needed...
    SELECT Name,  v2/v1 as div from (SELECT Val1 as v1, VAL2 as v2, name FROM mytable)
    ...and union the results of the 2 divisions, selecting and ordering as appropriate

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: MySQL Queries

    copeg, I took your post as a jumping point and found the solution for my first problem (have not looked into the second yet).
    In case anyone comes across this same problem, the solution for my first problem was:
    (SELECT `Name`, `Val2` AS value FROM `someTable` ORDER BY `Val2` DESC LIMIT 3) UNION (SELECT `Name`, `Val3` AS value FROM `someTable` ORDER BY `Val3` DESC LIMIT 3) ORDER BY value DESC LIMIT 3

    The statement in the first parentheses orders the items in Val2 and sets the ordered list with the name: "value". The statement in the second parentheses does the same with Val3. The UNION combines the two into one result. And the final ORDER BY statement, where it orders by "value", orders the UNIONed result by the combination of Val2 and Val3 (with the result set column alias being 'value').
    And, it DOES allow for repeats.

    I'm going to see if I can hammer out the second statement later today.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. mysql queries in a servlet...
    By csharp100 in forum JDBC & Databases
    Replies: 0
    Last Post: December 15th, 2012, 02:01 PM
  2. executing separate queries via threading
    By nischalinn in forum Threads
    Replies: 2
    Last Post: June 15th, 2012, 12:06 PM
  3. regarding nested queries in jsp
    By ravi_91 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 4th, 2011, 10:15 AM
  4. Help running SQL queries in Ubuntu
    By Mic[RSA] in forum Java IDEs
    Replies: 0
    Last Post: June 29th, 2010, 12:53 PM
  5. Hey, I'm new to Java and just have a few queries
    By Pseudonym134 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 5th, 2009, 06:44 PM