我在哪里做过失败是SQL语句? [英] Where have I made a Failure is SQL statement?

查看:75
本文介绍了我在哪里做过失败是SQL语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我希望通过过滤4列来从数据库中获取数据。第一列标题是Money(金额),第二列是Currency,但我在DB中另外有两个相同的列,例如:Money2和Currency2。因此,我希望获得所有数据形式db,其中Money超过100且Currency是USD,但另外我想获得Money2超过200且Currency2为EUR的所有数据。我有以下查询写,但不幸的是它没有给出第二个查询的结果。

So, I want to get datas from database by filtering 4 columns. First column title is Money(amount of money) and the second is Currency, but I have in DB additionally two same Columns with same definitions for example: Money2 and Currency2. So I want to get all datas form db where Money is more than 100 and Currency is USD, but additionally I want to get all datas where Money2 is more than 200 and Currency2 is EUR. I have following query written but unfortunately it doesn't give result from second query.

SELECT * FROM DB WHERE (Money > 100 AND Currency =  USD) OR Money2 > 200 AND Currency2 = EUR). 

我在哪里失败?

推荐答案

从你的查询(下面给出参考),我可以看到两个未命中:

From your query(given below for reference), I can see two misses:
SELECT * FROM DB WHERE (Money > 100 AND Currency =  USD) OR Money2 > 200 AND Currency2 = EUR). 



首先,该声明可能缺少一个开括号(在Money2之前,所以您可以尝试将其添加到选择查询中,例如:


First, the statement may be missing a open bracket "(" before Money2, so you can try adding that to the select query, like:

SELECT * FROM DB WHERE (Money > 100 AND Currency =  USD) OR (Money2 > 200 AND Currency2 = EUR) 



Second从数据中,我假设Currency和Currency2是varchar或char字段。您可能想尝试为这些输入添加单引号,例如:


Second, from the data, I assume Currency and Currency2 are varchar or char fields. You might want to try by adding single quotes for these inputs, like:

SELECT * FROM DB WHERE (Money > 100 AND Currency =  'USD') OR (Money2 > 200 AND Currency2 = 'EUR'). 


你不给我们任何例子您的数据库中的数据,或者说'失败'实际上是什么



我会冒一个猜测/暗示,'或Money2 ......'条款应该是'AND Money2 .....' - 你说
You dont give us any examples of the data in your DB, or say what the 'failure' actually is

I'd hazard one guess/hint, the 'OR Money2...' clause should be 'AND Money2.....' - you say
引用:

另外我想

这意味着你想要两套返回的数据,而不是你现在拥有的一个或另一个..



没有看到你的数据我也会想到你所需要的联合,如果你的数据满足同一记录中的两个约束,那该怎么办?理想情况下,你想要2行数据? yes / no / maybe?

which means you want both sets of data returned, not one or the other as you have now ..

without seeing your data I'd also be thinking wether a 'union' is what you need, what if you have data that satisfies both contraints in the same record - Ideally you would want 2 lines of data out ? yes/no/maybe ?


这个问题的解决方案是使用UNION,如下所示:SELECT * FROM DB WHERE Money> 100 AND Currency ='USD'UNION Money2> 200 AND Currency ='EUR'
Solution for this question is to use UNION like this : SELECT * FROM DB WHERE Money > 100 AND Currency = 'USD' UNION Money2 > 200 AND Currency = 'EUR'


这篇关于我在哪里做过失败是SQL语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆