简体   繁体   English

MySQL Select:什么时候还其他情况?

[英]MySQL Select: Case When Or Something Else?

First time poster, long-time reader. 第一次张贴,长期读者。 I have been teaching myself code othe past 6-8 months and I like to think that I am fairly getting decent at the PHP/Javascript/MySQL stack. 在过去的6-8个月里,我一直在自学代码,我想我对PHP / Javascript / MySQL堆栈的了解还不错。 I definitely love the community that Stack has built because it really helps to share problems with people who understand things a little better. 我绝对喜欢Stack建立的社区,因为它确实有助于与了解得更多的人分享问题。

So my question is probably relatively simple, but I am not sure how to use Case When...Then here. 所以我的问题可能相对简单,但是我不确定如何使用Case When ... Then。 (Or whether that is even correct to use!). (或者使用起来是否正确!)。 Basically what I want to say in my where clause is: if this column = 1, then also process this condition. 基本上,我想在where子句中说的是:如果此列= 1,则还要处理此条件。 Something like: 就像是:

SELECT product_name
FROM b2c_products, b2c_sellers
WHERE b2c_products.productId = b2c_sellers.product_Id AND CASE WHEN b2c_products.productType = 1 THEN b2c_sellers.seller_country = '$country' END;

I hope that gets the idea across - I know this is a simple problem, I just am so new to SQL! 我希望这个想法能够得到传播-我知道这是一个简单的问题,我对SQL还是很陌生! Thanks a LOT in advance. 在此先感谢很多。

Case statements work don't work in the way you're trying, they mainly for selectors, see http://dev.mysql.com/doc/refman/5.0/en/case.html or staticsan's answer. Case语句的工作方式与您尝试的方式不同,它们主要用于选择器,请参见http://dev.mysql.com/doc/refman/5.0/en/case.html或staticsan的答案。 You can use them in WHERE clauses, but generally it should be considered for data transformation. 您可以在WHERE子句中使用它们,但通常应将其用于数据转换。

MySQL does have the concept of IF statements: http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if which can be nested. MySQL确实具有IF语句的概念: http : //dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if可以嵌套。 For example 例如

1 = IF(b2c_products.productType = 1, IF(b2c_sellers.seller_country = '$country', 1, 0), 0)

As an alternative, you can achieve what you're after with nested with simple AND clauses which will allow you to use indexes too, eg 作为替代,您可以通过嵌套简单的AND子句来实现您的目标,该子句也将允许您使用索引,例如

SELECT product_name
FROM b2c_products, b2c_sellers
WHERE ((b2c_products.productType = 1 AND b2c_sellers.seller_country = '$country')
   OR b2c_products.productType <> 1);

As a side note, make sure you escape '$country' to avoid SQL injection. 附带说明,请确保转义'$ country'以避免SQL注入。

Actually, no, what you're trying to do is far from obvious. 实际上,不,您想要做的事情远非显而易见。

CASE is normally used in the fields of a SELECT statement (between SELECT and FROM ) to alter the returning data. CASE通常用于SELECT语句(在SELECTFROM之间)的字段中,以更改返回的数据。 A simple example might be: 一个简单的示例可能是:

SELECT userid, CASE WHEN disabled = 1 THEN 'Disabled' ELSE '' END AS disabled FROM users;

The way I think of how CASE works is that it runs on every row of output. 我对CASE工作方式的看法是,它运行在输出的每一行上。

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

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