如何以编程方式选择在WHERE条件中使用哪个列?

时间:2015-11-27 11:20:18

标签: mysql sql

我有一个表table,其中有三个字段:idfield1field2

我需要创建的SQL查询应该选择id s WHERE field1 LIKE 'Some string'但如果field1NULL我应该对field2执行过滤,在伪SQL中:

SELECT id FROM table
WHERE (IF `field1` IS NULL THEN `field2` ELSE `field1`) LIKE 'Some string'

我如何在MySQL中实现这一目标?

感谢您的关注!

4 个答案:

答案 0 :(得分:4)

您可以使用其中一个Flow Control Operators。你可以写:

CASE运营商(推荐)

SELECT id FROM table
WHERE CASE 
    WHEN `field1` IS NOT NULL THEN `field1`
    ELSE                           `field2`
END LIKE 'Some string'

IF构建

SELECT id FROM table
WHERE IF(`field1` IS NOT NULL, `field1`, `field2`) LIKE 'Some string'

IFNULL构建

SELECT id FROM table
WHERE IFNULL(`field1`, `field2`) LIKE 'Some string'

答案 1 :(得分:2)

或者:

class Route: CustomStringConvertible {

    let distance: Int
    var description: String { return "Route with distance: \(distance)" }

    init(distance: Int) {
        self.distance = distance
    }

}

let routes = [
    Route(distance: 20),
    Route(distance: 30),
    Route(distance: 10)
]

let maxRoute = routes.max(by: { (a, b) -> Bool in
    return a.distance < b.distance
})

print(String(describing: maxRoute)) // prints: Optional(Route with distance: 30)

答案 2 :(得分:0)

使用或:

public class ActivityHome extends Activity {
    String strLoggedInUserID;
    TextView tvData;
    Firebase fbRef = new Firebase("https://myurl.com/users");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        tvData = (TextView) findViewById(R.id.tvData);

        Query queryRef = fbRef.orderByChild("gender").equalTo("m");

        Bundle extras = getIntent().getExtras();
        strLoggedInUserID = extras.getString("userID");

        fbRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

        queryRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                //long longChildCount = dataSnapshot.getChildrenCount();

                // tvData.setText(dataSnapshot).toString();
                tvData.setText(s);

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            //tvData.setText(s);

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

    }
}

答案 3 :(得分:0)

您可以使用OR: SELECT id FROM table field1 LIKE&#39;某些字符串&#39;或field2 LIKE&#39;一些字符串&#39;