简体   繁体   English

SQL比较第一个或第二个值对

[英]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. 当我psi_agpm_a是因为有一个值对psi_bgpm_b ,我现在有运行两个SQL querys得到我需要正确地呈现我的网站页面的值,但是这导致两组结果被追加到该页面。

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: 这些函数构建的当前SQL如下:

flow_query(); 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. 第二个查询几乎相同,但是它使用psi_agpm_a作为值参数。 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 ? 是否有任何方法可以组合这些查询以返回将引用psi_agpm_a的单个结果集,并且如果未返回任何结果,则它将引用psi_bgpm_b

I am relatively novice to SQL so if this is not possible then I shall seek an alternative solution. 我是SQL的新手,所以如果不可能,那么我将寻求替代解决方案。

May as well call it an answer. 也可以称之为答案。 You can use and / or clauses in you where statement. 您可以在where语句中使用和/或子句。

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. 您还可以在select语句中放置一个case子句,该子句将向您显示在哪个where子句中找到了匹配项(如果需要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL第一个列表中的第二个 - SQL any of 1st list IN 2nd 如何在sql中比较同一表的第一条记录中的列A和第二条记录中的列B - How to compare columnA in 1st record and columnB in 2nd record of a same table in sql SQL ORDER BY 第一列,但如果第一列为空,则用第二列中的值替换空值 - SQL ORDER BY 1st column, but if 1st column is empty replace the empty value by a value from 2nd column 如何在另一个表中选择具有相同值的SQL表中的第一行和第二行实例? - How to select 1st and 2nd row instance in SQL table having the same value in another table? T-SQL从第一个选择的值在第二个选择联合的每一行 - T-SQL Value from 1st select on each row in 2nd select of a union all 在 sql 中查找运行总计 - 如果第一列没有值,则从第二列计算 - Find Running Total In sql - if 1st column has no value then calculate from 2nd column 如何连接两个SQL表,将第二个表中的值放入第一个表中的列? - How can I join two SQL tables, putting a value from the 2nd table into a column in the 1st? SQL查询:在第1和第2日期之前进行数据透视 - SQL query: pivot data by 1st and 2nd date SQL连接2个表,从第2个条件查询第1个表 - SQL join 2 tables, query 1st with condition from 2nd SQL查询与第一和第二最高薪水有关 - Sql query related to 1st and 2nd highest salary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM