We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Help with SQL Query

khawaib
khawaib asked
on
Medium Priority
303 Views
Last Modified: 2013-12-12
Hi,

I am struggling with this query.

I have a table in database with three columns:
weight       uk       international
220              0      3.20
240              0      3.50
260              0      3.70
280              0      3.90

I need a query that can take the weight value and return me that row in table. For example if weight is 223, it shud return me row with 3.20

Thanks
Comment
Watch Question

Commented:
SELECT * FROM TableName WHERE weight='220'

Commented:
Err sorry, typo...

SELECT * FROM TableName WHERE weight='223'
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
if the data type of the field weight is numeric, do not use quotes.
for the rest, the query is correct, although you should avoid using SELECT *, instead specify the columns you need instead
SELECT uk, international FROM TableName WHERE weight= 220

Open in new window

Commented:
Try -
DECLARE @weight int
SELECT @weight = 220
 
SELECT * FROM table1
WHERE Weight =  (SELECT MAX(weight) from table1 WHERE Weight <= @weight)

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
....

SELECT TOP 1 weigth, uk, international FROM TableName WHERE weight <= 223 ORDER BY weigth DESC

Open in new window

Author

Commented:
@angelIII and yodercm:
Queries wont work as there is no row 223 in table and I do not have value 220.

@reb73:
I will try your solution and seems it will work.
Can you please explain what does (SELECT MAX(weight) from table1 WHERE Weight <= @weight) do?

By the way I am using it with PHP.

Thanks
Angels this answer is right

SELECT TOP 1 weigth, uk, international FROM TableName WHERE weight <= 223 ORDER BY weigth DESC

Commented:
@angel ... if the data field is numeric, the quotes shouldn't affect anything.  Is there a reason you suggest it matters?

@khawaib ... angeliii is correct that it's slightly more computer-efficient to specify each field, I use * most of the time because it's more programmer-efficient, and because it doesn't require modification and introduces fewer bugs when you need an additional field.

Please remember to share the points among all the helpful/correct answers, not just give them to the first person who posted :)

Commented:
Ah, I see, you stated that you wanted the row with value 223, which might not exist, not that you wanted the row with the next larger value.

Let me just correct the spelling in AngelIII's query

SELECT TOP 1 weight, uk, international FROM TableName WHERE weight <= 223 ORDER BY weight DESC

Commented:
"Can you please explain what does (SELECT MAX(weight) from table1 WHERE Weight <= @weight) do?"

This subquery locates the maximum value from the field weight where the value is less than or equal to the parameter(which seems to be your requirement) and passes this value to the outer query which fetches the correct row..
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>@angel ... if the data field is numeric, the quotes shouldn't affect anything.  Is there a reason you suggest it matters?

MySQL will refuse to work correctly, and Oracle will eventually ignore indexes on the field if you specify data with the wrong data type: '223' is text, and not numeric.

now, with the MySQL zone specified (which I had not seen before), the TOP syntax is wrong..
it would then be:

SELECT weight, uk, international FROM TableName WHERE weight <= 223 ORDER BY weight DESC LIMIT 1

Open in new window

Commented:
^ AngelIII

I wasn't sure if the TOP would work in MySQL either, that why I used the subquery in my first post.. ;-)

Commented:
TOP and LIMIT may be efficient if there is an index on the weight column, but will force a sort in the execution plan which can be more costly than a tablescan in a table with numerous rows..

I just ran an equivalent query in a table with 337000 rows, the execution plan (SQL 2000 though and not MYSQL) showed the TOP 1 syntax was 81% more costly than the subquery  syntax..

Author

Commented:
Query with TOP was returning me empty.


This seems to be working for me. Will close the question after more teting.
$query= "SELECT * FROM `postage` WHERE `weight` = (SELECT MAX(weight) from `postage` WHERE weight <= '".$weight_total."')";

Open in new window

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
TOP cannot work on mySQL. so, please also try my version with LIMIT.

but of course, both versions will profit from an index on the weight column

Author

Commented:
Sorry my skills are not very good with SQL. By index you mean making wight primary key?
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>By index you mean making wight primary key?
no, index means index.
"primary key" is a special form of index, aka a unique index (unique constraint).

now, in mysql, when you say "key", that's an index.
so, a "key" is not a primary key.


Author

Commented:
thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.