简体   繁体   中英

Nested query sql , unable to execute

I want to compare some floating values . To do,my query is like :

SELECT * FROM pricelist WHERE START_FREQUENCY >= '(
    SELECT START_FREQUENCY
    FROM pricelist
    WHERE START_FREQUENCY BETWEEN '0.49999' AND '0.50001'
)' AND STOP_FREQUENCY <= '(
    SELECT STOP_FREQUENCY
    FROM pricelist
    WHERE STOP_FREQUENCY BETWEEN '0.59999' AND '0.60001'
)'

But in my PHP it is showing 'Unable to execute query' . Where I am doing wrong ? Please help

EDIT :

I have a table where there is two fields , START_FREQUENCY and STOP_FREQUENCY . I am trying to extract the all the values with that comes within frequency range . For example say

  START_FREQUENCY STOP_FREQUENCY
1            0.4            0.9
2            0.5            1.4
3            0.8            1.9

When the user enter start frequency = 0.4 and stop frequency = 1.9 he should get results of Row1 , Row2 and Row3 .

Your inner SELECTs shouldn't be quoted. Try this:

SELECT * FROM pricelist WHERE START_FREQUENCY >= (
    SELECT START_FREQUENCY
    FROM pricelist
    WHERE START_FREQUENCY BETWEEN 0.49999 AND 0.50001
) AND STOP_FREQUENCY <= (
    SELECT STOP_FREQUENCY
    FROM pricelist
    WHERE STOP_FREQUENCY BETWEEN 0.59999 AND 0.60001
)

I also removed the quotes around your numbers; MySQL will convert them to numbers behind your back but there's no need to make the database guess and do more work.

Your version has this structure:

SELECT *
FROM pricelist
WHERE START_FREQUENCY >= '...'0.49999' AND '0.50001'...'
  AND STOP_FREQUENCY  <= '...'0.59999' AND '0.60001'...'

And 'string'0.49999' isn't valid.


Update: Sounds like you're just overcomplicating this. I think you just want this (using your 0.4 and 1.9):

select *
from pricelist
where START_FREQUENCY >= 0.4
  and STOP_FREQUENCY  <= 1.9

Try your query in the MySQL command-line client or another more direct environment so you can see the actual error being generated by the database.

Most likely it has to do with your use of quotes around your subqueries, which shouldn't be there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM