简体   繁体   中英

SQL Compare on 1st or 2nd value pair

I need to build a query which will compare off one of two value pairs in my table, my table structure looks something like this:

   product_id   |  psi_a   |   gpm_a   |   psi_b   |   gpm_b   |
   -------------------------------------------------------------
   PRODUCT_123  |   1000   |    400    |   8000    |    300    |
   -------------------------------------------------------------
   PRODUCT_456  |  2804    |   3006    |   5800    |    579    |

When my psi_a and gpm_a are a value pair as are psi_b and gpm_b , I currently have to run two SQL querys to get the values I require to render my site page correctly, however this results in two sets of results being appended to the page.

Markup

$flowQ = $function->flow_query( $pType, $pVal, $gVal, $class_style, $cVal, $pageCat );
$highQ = $function->high_query( $pType, $pVal, $gVal, $class_style, $cVal, $pageCat );

if(empty($flowQ)===false){
   $function->generate_view( $flowQ, $pType, $pVal, $gVal, $class_style, $cVal, $pageCat );
}

The current SQL built by these functions are as follow:

flow_query();

$query = $this->db->prepare( "SELECT * FROM `pumps` WHERE `pump_type` = ? AND `psi_a` >= ? AND  gpm_a >= ? AND `pump_category` = ? ORDER BY pump_type DESC" );
$query->bindValue(1, $pType);
$query->bindValue(2, $pVal);
$query->bindValue(3, $gVal);
$query->bindValue(4, $cVal);

The second query is pretty much identical, but it uses psi_a and gpm_a as value parameters. Is there any way to combine these querys to return a single result set that will reference psi_a and gpm_a , and if that returns no results then it references psi_b and gpm_b ?

I am relatively novice to SQL so if this is not possible then I shall seek an alternative solution.

May as well call it an answer. You can use and / or clauses in you where statement.

where (psi_a = ? and gpm_a = ? ) or (psi_b = ? and gpm_b = ? )

You can also put a case clause in the select statement that will show you which where clause found the match if it's needed.

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