where-clause

Doctrine: Multiple (whereIn OR whereIn) query?

荒凉一梦 提交于 2019-12-03 03:17:31
I'm having trouble crafting a fairly simple query with Doctrine... I have two arrays ($countries, $cities) and I need to check whether database record values would match any inside either. I'm looking for something like: ->whereIn('country', 'city', $countries, $cities) ... with 'country' being a WHERE IN for $countries and 'city' being a WHERE IN for $city. I could separate the two out but the needed query has lots of other conditions so that's not possible. The resulting SQL I'm after would be: SELECT ... WHERE ... AND ... AND ... AND ('country' IN (1,2,3) OR 'city' IN (7,8,9)) AND ... AND .

T-SQL Where Clause Case Statement Optimization (optional parameters to StoredProc)

荒凉一梦 提交于 2019-12-03 02:56:56
I've been battling this one for a while now. I have a stored proc that takes in 3 parameters that are used to filter. If a specific value is passed in, I want to filter on that. If -1 is passed in, give me all. I've tried it the following two ways: First way: SELECT field1, field2...etc FROM my_view WHERE parm1 = CASE WHEN @PARM1= -1 THEN parm1 ELSE @PARM1 END AND parm2 = CASE WHEN @PARM2 = -1 THEN parm2 ELSE @PARM2 END AND parm3 = CASE WHEN @PARM3 = -1 THEN parm3 ELSE @PARM3 END Second Way: SELECT field1, field2...etc FROM my_view WHERE (@PARM1 = -1 OR parm1 = @PARM1) AND (@PARM2 = -1 OR

SQL: How can I update a value on a column only if that value is null?

回眸只為那壹抹淺笑 提交于 2019-12-03 02:25:47
I have an SQL question which may be basic to some but is confusing me. Here is an example of column names for a table 'Person': PersonalID, FirstName, LastName, Car, HairColour, FavDrink, FavFood Let's say that I input the row: 121312, Rayna, Pieterson, BMW123d, Brown, NULL, NULL Now I want to update the values for this person, but only if the new value is not null, Update: 121312, Rayna, Pieterson, NULL, Blonde, Fanta, NULL The new row needs to be: 121312, Rayna, Pieterson, BMW123d, Blonde, Fanta, NULL So I was thinking something along the lines of: Update Person(PersonalID, FirstName,

LINQ Join Where Clause

拥有回忆 提交于 2019-12-02 22:17:33
I'm struggling with a join/where clause with what is a rather simple sql select statement. I am trying to retrieve a list of product information from tb1 with the where condition behind situated in tbl2 but this must be joined by three different columns. so the SQL would look something along the lines of: SELECT tb1.* FROM tb2 INNER JOIN tb1 ON tb2.Col1 = tb1. Col1 AND tb2.Col2 = tb1. Col2 AND tb2.Col3 = tb1.Col3 WHERE (tb2.Col1 = col1) AND (tb2.Col2 = col2) AND (tb2.Col4 = string) ColX is the main where clause with the string to be passed in as parameter; all other columns are within the

How do I create a conditional WHERE clause?

六眼飞鱼酱① 提交于 2019-12-02 21:29:18
I need to have a conditional where clause that operates as so: Select * From Table If (@booleanResult) Begin Where Column1 = 'value1' End Else Begin Where column1 = 'value1' and column2 = 'value2' End Any help would be appreciated. Could you just do the following? SELECT * FROM Table WHERE (@booleanResult = 1 AND Column1 = 'value1') OR (@booleanResult = 0 AND Column1 = 'value1' AND Column2 = 'value2') You can group conditions easily in a WHERE clause: WHERE (@BooleanResult=1 AND Column1 = 'value1') OR (@BooleanResult=0 AND Column1 = 'value1' AND column2 = 'value2') Based on the script in

Laravel where on relationship object

那年仲夏 提交于 2019-12-02 20:00:18
I'm developing a web API with Laravel 5.0 but I'm not sure about a specific query I'm trying to build. My classes are as follows: class Event extends Model { protected $table = 'events'; public $timestamps = false; public function participants() { return $this->hasMany('App\Participant', 'IDEvent', 'ID'); } public function owner() { return $this->hasOne('App\User', 'ID', 'IDOwner'); } } and class Participant extends Model { protected $table = 'participants'; public $timestamps = false; public function user() { return $this->belongTo('App\User', 'IDUser', 'ID'); } public function event() {

MySQL query with multiple 'OR' statements

♀尐吖头ヾ 提交于 2019-12-02 19:53:12
问题 I have a query, that I have tested outside of PHP directly on SQL and still getting the same problem The idea being that $currentArtist is sent through using the GET method, where this value matches the value in the event_artist columns the information is displayed. SELECT * FROM `".DBN."`.`events` WHERE `event_artist1` OR `event_artist2` OR `event_artist3` = '".$currentArtist."' "; The problem I have is that it is completely ignoring the WHERE part of the query. It just returns the complete

Syntax error in WHERE clause near '?) AND (Date = ?)'

不想你离开。 提交于 2019-12-02 17:45:46
问题 I am trying to send a SQL prepared statement to MySQL DB. This is what I have: String sql1 = "SELECT idReimbursed_Funds As idReimFunds FROM reimbursedfunds Where ReimFundsName = ? AND Date = ?"; PreparedStatement pstmt1 = conn.prepareStatement(sql1); pstmt1.setString(1, reimfund.getReimFundsName()); pstmt1.setDate(2, (Date) reimfund.getDate()); ResultSet rs1 = pstmt1.executeQuery(sql1); while(rs1.next()){ idReimFunds = rs1.getInt("idReimFunds"); } After googling this problem, I found

Oracle - Can A Bind Variable Be Put In In() Clause If The Variable Can Be Empty

风流意气都作罢 提交于 2019-12-02 10:21:55
问题 In Oracle , can a bind variable be put in IN() clause if it can be empty? Asking because in the SQL if Oracle see IN() without any data in it, it shows error ORA-00936: missing expression. Edit: Failed to mention that no PL/SQL can be used... Edit2: Also failed to mention that the variable is either in format 1,2,3 or empty string. The empty string cannot be replaced with NULL. 回答1: Seems to be working: declare l_statement varchar2(32767); begin l_statement := 'select * from user_tables where

how to ignore a condition in where clause

本秂侑毒 提交于 2019-12-02 09:41:02
问题 SELECT field1 FROM table1 WHERE field1 >= 4006 AND field1 < ( SELECT field1 FROM table WHERE field1 > 4006 AND field2 = false ORDER BY field1 LIMIT 1 ) i want the second condition ( AND field1 < ) to be ignore if the inner select returned no record. Related to this topic 回答1: Something like (untested!): SELECT field1 FROM table1 WHERE field1 >= 4006 AND (field1 < ( SELECT field1 FROM table WHERE field1 > 4006 AND field2 = false ORDER BY field1 LIMIT 1 ) OR NOT EXISTS ( SELECT field1 FROM