Questions tagged [condition]
81 questions
39
votes
5 answers
Logical operators OR AND in condition and order of conditions in WHERE
Let's examine these two statements:
IF (CONDITION 1) OR (CONDITION 2)
...
IF (CONDITION 3) AND (CONDITION 4)
...
If CONDITION 1 is TRUE, will CONDITION 2 be checked?
If CONDITION 3 is FALSE, will CONDITION 4 be checked?
What about conditions on…
garik
- 6,782
- 10
- 44
- 56
16
votes
2 answers
NOT (a=1 AND b=1) vs (a<>1 AND b<>1)
In the WHERE clause of a SQL query I would expect these two conditions to have the same behavior:
NOT (a=1 AND b=1)
vs
a<>1 AND b<>1
The first condition behaves as expected, and while I epxect the second condition to do the same thing, it does…
jon
- 177
- 1
- 3
15
votes
3 answers
How to export mysql database based on a where condition
I need to export the data and structure of a table but the data must have a specific condition (WHERE status=0 and id>20).
How to export mysql database based on a where condition from phpMyAdmin or anything.
Somnath Muluk
- 1,104
- 3
- 13
- 15
13
votes
2 answers
Conditional string concatenation in PostgreSQL
I have a table parcels which currently contains the columns owner_addr1, owner_addr2, owner_addr3. Sometimes, one or both of the latter two fields is empty. I want to combine them into a single new field, owner_addr where each of the above fields is…
J. Taylor
- 379
- 2
- 5
- 17
11
votes
1 answer
How to conditionally raise an error in MySQL without stored procedure
I need to conditionally raise an error, but I can only use a simple statement and no stored procedure.
I'd like to do something like this:
select case when foo = "bar" then 1 else SIGNAL SQLSTATE 'ERROR' end;
Unfortunately SIGNAL is only usable in…
Gene Vincent
- 222
- 3
- 14
11
votes
4 answers
Invert a Boolean expression which can return UNKNOWN
Example
I have a table
ID myField
------------
1 someValue
2 NULL
3 someOtherValue
and a T-SQL Boolean expression which can evaluate to TRUE, FALSE or (due to SQL's ternary logic) UNKNOWN:
SELECT * FROM myTable WHERE myField =…
Heinzi
- 3,210
- 2
- 32
- 43
7
votes
3 answers
Convert date in date list condition to list of date ranges
I want to look for all the records that occur on specific dates.
SELECT *
FROM table1
WHERE date(column) in ($date1, $date2, ...);
However, as many of you, know this kind of comparison doesn't get along with indexes. So, I was wondering if there…
msemelman
- 123
- 6
6
votes
1 answer
Change constraint on column based on value of another
Is it possible to change a constraint on a column in postgres based on the value of another column?
E.g. (pseudocode):
CREATE TABLE transactions(
id SERIAL PRIMARY KEY,
type TXN_TYPE NOT NULL,
amount BIGINT,
. . . .,
refunded…
Kinnard Hockenhull
- 165
- 2
- 5
6
votes
2 answers
How to use a conditional ORDER BY in MySQL?
I have the table courses where we have some fields and also the field course(string), favorited(bool), favoritedDate(dateTime), dateUpload.
The teacher uploads the course and this date is stored in the dateUpload field. If the teacher flag this…
IgorAlves
- 337
- 3
- 5
- 16
5
votes
1 answer
Search a Redis Database
Imagine having a tun of hashes in a Redis Database, each representing a user object. How would you get the user with for example the lowest registration date? The user where ts_registered is the lowest. I searched a bit on the Commands Page on…
Jeroen
- 163
- 2
- 8
5
votes
1 answer
Does the "don't use errors for flow control" axiom apply to postgres?
In traditional programming there is an axiom that states, "do not use errors for flow control". A general example is to throw an error then catch the error instead of using an ordinary conditional statement or break statement. This is harmful…
Freiheit
- 249
- 3
- 18
5
votes
2 answers
How do I select arrays that are not empty?
Why is this so tricky, what is token set to that it isn't equal to null nor an empty string?
SELECT lexemes
FROM ts_debug('This is a title')
WHERE alias = 'asciiword';
lexemes
---------
{}
{}
{}
{titl}
(4 rows)
Ok.. So I want to get rid of…
Evan Carroll
- 65,432
- 50
- 254
- 507
4
votes
1 answer
Conditional selection in FROM clause
I have two tables:
parent with columns identifier (pkey) and period;
child with columns identifier (pkey), parent_identifier and period.
There are additional columns, but I did not list them here as they are not really relevant.
I want to select…
pbillen
- 301
- 2
- 6
3
votes
1 answer
PostgreSQL WHERE clause with two OR'ed conditions, priority to first
I'm storing user data (username, email, password hash) in PostgreSQL, and I can only enforce uniqueness on username. I'd like to let users provide either their username or their email when they login. However, when matching their input against…
Lorenz Forvang
- 45
- 1
- 4
3
votes
4 answers
MySQL : Conditional ORDER BY to only one column
Hello and thanks for taking time to read this question.
I am using MySQL, and I want to sort results using ORDER BY to one specific column, but the results must be ordered according an specific criteria to this column. For example, to the following…
MWt
- 143
- 1
- 5