Questions tagged [condition]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
26 views

Can conditionals be used in a WHERE condition?

I am working on something and found this SQL that I am not sure how to rewrite correctly, meaning using PHP PDO. The SQL looks like: $sql = 'SELECT * FROM table WHERE column ' . isset($variable) ? . '=...
ReynierPM's user avatar
  • 1,764
0 votes
1 answer
44 views

Mariadb using IF in trigger to fill null value on insert

Trying to use a trigger to a) fill in a new column top_category with values left of a colon, found in category, then b) If there is no colon, then put entire insert value into top_category. I can't ...
JohnZastrow's user avatar
0 votes
0 answers
189 views

Oracle 19c - Multiple If Conditions

create or replace TRIGGER trigger_name ... ... BEGIN IF SYS_CONTEXT (...) IN (...) THEN IF SYS_CONTEXT (...) NOT IN (...) THEN IF SYS_CONTEXT (...) NOT IN (...) THEN INSERT INTO owner.table_name (...) ...
jrdba123's user avatar
0 votes
1 answer
1k views

MySQL Trigger to prevent update in one table if particular data is available in another table

I need a MySQL trigger that can prevent update of a particular column data, if some specific data is available in another table. I have two tables product and sales. Product Sale I need to prevent ...
PRAMOD MANN's user avatar
1 vote
2 answers
436 views

Applying conditional statement in Oracle DB

In the Oracle DB there are three tables, see below. Table: 'People' Table: 'Dogs' Table: 'Cats' Here is a Fiddle with data to test : http://sqlfiddle.com/#!4/d7cb4 Depending on the result of a ...
Taras's user avatar
  • 177
2 votes
1 answer
519 views

Splitting up a few big tables into multiple ones and avoiding empty tables

I am in the process of restructurizing and unifying a large database in PostgreSQL. One part of this is to split up some large tables into smaller ones according to their value in 'key'. The function ...
king_of_limes's user avatar
0 votes
0 answers
22 views

Do most real world join conditions involve no more than 2 columns?

In the database application I have worked on, all join conditions are binary, like select * from A join B join C on A.id=B.id and B.orderno = C.orderno Each condition, separated by "and", ...
larryliu's user avatar
0 votes
1 answer
132 views

Stuck at inserting records into MariaDB conditionally

I know about insert ignore. That's of no use, because MariaDB does not consider NULL to be part of the uniqude index. Therefore I have to use if ... else. But this code does not run: select * from ...
Saeed Neamati's user avatar
-1 votes
2 answers
260 views

How to conditionally pick a row based on status value?

I want to select all available and bought packs. An user can open two pages to buy one pack, this results in two rows in the payment table. One gets status 'Pending' and the one that is bought gets ...
KarelDeGrote's user avatar
0 votes
0 answers
55 views

using lead() to modify next row data

So I have some data like this: And what I would like to do is modify the WorkDate using the following logic if lead(EndTime) = StartTime then lead(WorkDate) = WorkDate So for example R3 WorkDate ...
Freud's user avatar
  • 1
0 votes
1 answer
309 views

Fetch all rows and leave column value empty if no equality match

I have a query where I want to fetch all rows where the value in column x is between 400-600. Including in this I want a matching column value from antoher table. But I want all 200 rows between 400 ...
Burry's user avatar
  • 103
0 votes
3 answers
811 views

A flag (indicator) that shows for each row which conditions was used to select it

Suppose I have the table foo. I want to filter the rows of this table using different conditions (cond_1, cond_2, ..., cond_n - the order is important) combined in a disjunction, i.e.: SELECT * FROM ...
Radu Dumbrăveanu's user avatar
0 votes
1 answer
1k views

Create a trigger to fill a field according to acondition in sql

I want to create a trigger to fill a field according to acondition as below description For example: I have a table for all insps that have attachments named: “inspectionAttachments” and I have ...
lubna.najeh's user avatar
1 vote
2 answers
8k views

Set column values based on condition statement

I want to add values to a new column based on: if board_stage is less than alight_stage then service_direction equals 1, to indicate Inbound; if not, then service_direction equals 0, indicating ...
Nuala Carvill's user avatar
2 votes
2 answers
61 views

Where Clause for Results Appearing after Occurence

I'm wondering if it's possible to find records that only appear after the first occurrence of a where clause. I'm currently using PostgreSQL for this example but something not PostgreSQL specific ...
TheLovelySausage's user avatar
0 votes
1 answer
75 views

return field name and number of rows which meet condition in one sentence

I have a table in postgres with 10 fields, and need to check for certain values in 3 of them, then return the field name which met the condition and the number of rows in each field. So it's something ...
Matias's user avatar
  • 141
2 votes
1 answer
1k views

How to filter table 2 before joining to table 1 so that the result still includes all of table 1, even if no match in table 2?

The following returns all rows from table 1 where post_type = dlm_download. It groups like values from table 2 by the matching column in table 1 so that each row in table 1 has a maximum of one ...
user2461674's user avatar
0 votes
1 answer
129 views

Prevent failure in conditional insert in mysql database

The infrastructure of our system looks like this. An AWS lambda function receives requests such as (accountId, .....). It creates an entry in the MySQL database using a newly generated UUID as caseId....
Swagatika's user avatar
  • 101
-3 votes
2 answers
189 views

Multiple statement conditions using OR AND within the query

I have this survey table and I'm trying to gather the information of userid who answer Strongly agree to the question of I'm trying to watch my weight OR I love drinking Coffee Black AND must have ...
learning's user avatar
4 votes
1 answer
374 views

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 ...
pbillen's user avatar
  • 291
0 votes
0 answers
167 views

Condition table theory

Is there a good way to create a "table of conditions"? I have a huge table, where 4-5 columns are considered in a set of rules, and based on those columns default values must appear in other columns. ...
vacip's user avatar
  • 123
0 votes
1 answer
740 views

Conditional Join in DB2

2 tables are given Table T_PRICE P_Price P_Type P_Route 15 red long 10 long 5 small Table T_ROUTE R_Type R_Route blue long red ...
Butterfly's user avatar
-2 votes
3 answers
1k views

Can a `Select` Subquery be conditionally returned? [closed]

Is it possible in Postgres to return a column(Resulting from a subquery) only when a condition matches? Query: SELECT (SELECT NOW()) AS FIRST, (SELECT NOW()) AS SECOND Outputs: | first | ...
Nick's user avatar
  • 107
5 votes
1 answer
407 views

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 ...
Freiheit's user avatar
  • 259
0 votes
2 answers
95 views

Get slave status using sql query inside if-else condition

How can I stop the slave if it is already running using sql query statements only? So far I tried to do something like below but getting sql syntax error. I think it is due to the borders of result ...
SkyRar's user avatar
  • 125
0 votes
1 answer
2k views

perform multiple conditional join on multiple tables

I have a situation where I need to get result based on conditional joins For example, SELECT CGI.SALES_BRAND_BSNSS_ID, CGI.SALES_BRAND_CD, NVL(MTRX. matrix_score_cd,'RV1') ...
heye's user avatar
  • 129
3 votes
3 answers
427 views

How to use IN clause in sql server so that it selects everything if nothing is provided for its list argument?

Assume I have a sql query like this SELECT * FROM TABLE where country in ('USA', 'UK') This query works as expected that is it selects all rows where country column is either 'USA' or 'UK'. Now ...
theprogrammer's user avatar
0 votes
2 answers
224 views

Postgres - Only allow insert if TWO columns match with a separate table

I have three tables: a users table, a books table, and a chapters table. Each book has an id called identifier_id (for good business reasons that I cannot work around). This id is guaranteed to be ...
JapanRob's user avatar
  • 103
1 vote
1 answer
806 views

In where clause, does MySQL continue checking conditions if first condition before AND falsy

I have a question about and AND inside a where clause of a query. Within the where clause, I have a subquery but it is after a few other, row value conditions, like validated = 1 AND (subquery). I ...
Ice76's user avatar
  • 133
2 votes
1 answer
984 views

IS NOT DISTINCT FROM vs row-wise equality with =

Looking at the below query, SELECT null IS NOT DISTINCT FROM null AS indf, null = null AS eq; indf | eq ------+---- t | From this we can see the result of the IS NOT DISTINCT FROM is true, ...
Evan Carroll's user avatar
  • 62.7k
1 vote
1 answer
3k views

MySql sort conditional based on column values

I have a table with products where there's a column for price and stock. I want the products to be display sorted by the following rules: All products with price = 0 go to the bottom; Then the ...
The Berga's user avatar
  • 113
1 vote
0 answers
96 views

How can I auto_increment main and sub category conditionnaly?

So I have a table of items as so |mainCtgry |subCtgry| --------------------- | 1 | 1 | --------------------- | 1 | 2 | --------------------- | 1 | 3 | -------------...
JSmith's user avatar
  • 143
1 vote
1 answer
3k views

Conditional where for postgres stored procedure

I want to create a conditional where clause using a PostgreSQL stored procedure. This is my original stored procedure : select distinct t.serialnumber as sn, t.productid as tid, ...
yozawiratama's user avatar
2 votes
2 answers
837 views

In what direction SQL Server WHERE clause conditions are checked?

I have this query: SELECT RCodeID as 'ListItemID', RCode as 'ListItemName' FROM RCode WHERE RCode not like '%,%' AND ISNUMERIC(RCode) = 1 AND CAST(RCode as int) >= CAST(@...
Aishwarya Shiva's user avatar
3 votes
0 answers
6k views

How to apply a condition on "count over partition"?

I'm using Oracle 11g. Assuming multiple consecutive entries (sorted by datetime) with the same place_id represent a single visit. With this table: datetime_id | user_id | place_id and I want to ...
Fadi Bakoura's user avatar
1 vote
1 answer
13k views

If else statement on join query

i am working with a Postgresql database having two tables: professionals[professional_id,user_id,account_id] users[user_id email] accounts[account_id email] professionals [professional_id,...
Medo's user avatar
  • 37
0 votes
1 answer
131 views

MySQL select all records unless user must be present in favorites

I am having trouble to find the best solution for the following problem; Description: I want to select all records from an items table, unless it is for favorites only, in that case the user must be ...
Kreshnik Hasanaj's user avatar
12 votes
2 answers
19k views

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 ...
J. Taylor's user avatar
  • 379
6 votes
2 answers
12k views

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 ...
IgorAlves's user avatar
  • 327
-1 votes
1 answer
41 views

If true then insert event [closed]

I have an insert statement that inserts events into a table. I need it to NOT insert if these events contain specific words or even specific username executing the event. Thought this would be easy ...
CrashWave's user avatar
0 votes
1 answer
1k views

SSAS Calculated Member Conditional Count

I have a table representing Accidents and a column with Accident Severity ranging from 'Slight' to 'Fatal'. I want to create a measure counting Fatal Accidents depending on other columns on the ...
SGuard's user avatar
  • 99
5 votes
1 answer
24k views

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 --------- {} {} {}...
Evan Carroll's user avatar
  • 62.7k
2 votes
1 answer
597 views

Matching multiple rows in a MySQL database

A custom built eCommerce store has the following two tables: orders | order_id | user_id | amount | shipping_info | date_time | |----------|---------|--------|---------------|--------------...
confusedab's user avatar
1 vote
1 answer
230 views

Is it possible to switch queries based on an input in mySQL?

Is there a way to switch queries based on values? Let's say I have the below query: select pdate as 'Payment Date', ROUND(@sum_amount := @sum_amount + `amount`) AS 'Total Amount' from ( select @...
Alireza's user avatar
  • 3,636
16 votes
2 answers
6k views

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 ...
jon's user avatar
  • 177
2 votes
2 answers
773 views

If a record does not exist set a value

I am struggling with my statement in PostgreSQL. I have 3 tables: tbl - flags (labeled as a) a.email tbl - accounts (labeled as b) b.id b.email tbl - foo (labeled as c) c.account_id c.total My sql ...
Michal N.'s user avatar
2 votes
2 answers
1k views

Updating whole table once, criteria : column_name and each value

I have a table with 606 rows. The first column is simply ID (starting at 1 to 606), the next column names are name1, other1, name2, other2, etc. until name100, other100. I'm trying to create a query ...
NewMilenium's user avatar
0 votes
2 answers
4k views

How to combine "SELECT" and "REPLACE INTO" when combining two MySQL tables into another?

I've got data with measurements split between two different tables, but the data doesn't need to be present in both: mysql> SELECT * FROM t1; mysql> SELECT * FROM t2; +----------------...
user1717828's user avatar
1 vote
0 answers
146 views

Tracking Changes

I have an SSRS report with hierarchies for the company and their contact info. As these hierarchies change overtime, is there a way to track what has changed since the last run(run on a weekly basis)? ...
Utrillion's user avatar
2 votes
1 answer
1k views

Conditional relationships? One-to-many relationship using a pivot table for only a type of entity

I have the following tables: users: - id - name - user_type_id (references id on user_types) neighborhoods: - id - name homes: - id - house_number - neighborhood_id (references id ...
Vic's user avatar
  • 165