Multiple where clauses


Priya

I have the following table:

 ID    Key   Value
 1     From  x
 1     To    Y
 1     CC    a
 2     From  Z
 2     To    X
 2     CC    b
 3     From  X
 3     To    Y  
 3     CC    c
 4     From  X
 4     To    Z
 4     CC    d

I want Id 1 and 3 as result such that where key="From" and value="x" and key="To" and value="Y" (From->To should be x->y) like this thing

SELECT Id
FROM table_name
WHERE REGEXP_LIKE(KEY, '(^|\s)from(\s|$)', 'i')
    AND REGEXP_LIKE(value, '(^|\s)x(\s|$)', 'i')
    AND
WHERE
WHERE REGEXP_LIKE(KEY, '(^|\s)to(\s|$)', 'i')
    AND REGEXP_LIKE(value, '(^|\s)y(\s|$)', 'i')

Is it possible to fix this problem?

Juan Carlos Oropesa

Conditions of UseSUM

SqlFiddleDemo

SELECT ID
FROM Table1
GROUP BY ID
HAVING 
      SUM(CASE WHEN "Key" = 'From' and LOWER("Value") = 'x' THEN 1 ELSE 0 END) = 1 
 AND  SUM(CASE WHEN "Key" = 'To' and LOWER("Value") = 'y' THEN 1 ELSE 0 END) = 1 

output

| ID |
|----|
|  1 |
|  3 |

Related


Laravel multiple WHERE clauses

Lakmal Premaratne I need to add multiple where clauses to a Laravel SQL query. My PHP code so far is: date_default_timezone_set('America/Los_Angeles'); $today = getdate(); $year = $today['year']; $month = $today['mon']; $day = $today['mday']; $today_ = $day.

Multiple where in clauses in Mysql

beginner When using the following query, I am getting incorrect results. How to fix this when using multiple where in clauses and HAVING. SELECT * FROM `otc_employee_qualifications` WHERE `emp_qualifctn_type` IN ( '26', '27' ) AND `qualification_value`

Multiple where clauses, Laravel

Bisebiu I've tried querying with 2 where clauses but the response is not correct, not sure why. $history = AnswerHistory::where('question_id', '=', $id) ->where(function ($query) { $query->where('answer_type', '!=', "skipped"); })

Multiple where clauses, Laravel

Bisebiu I've tried querying with 2 where clauses but the response is not correct, not sure why. $history = AnswerHistory::where('question_id', '=', $id) ->where(function ($query) { $query->where('answer_type', '!=', "skipped"); })

Using multiple clauses in where

John What is the correct way to include multiple wheres in a LINQ call OR List<Pos> posList = DbContext.PosList .Where<Pos>(p => p.Pos == "51000785" || p => p.Pos == "123")

Laravel multiple WHERE clauses

Lakmal Premaratne I need to add multiple where clauses to a Laravel SQL query. My PHP code so far is: date_default_timezone_set('America/Los_Angeles'); $today = getdate(); $year = $today['year']; $month = $today['mon']; $day = $today['mday']; $today_ = $day.

Laravel multiple WHERE clauses

Lakmal Premaratne I need to add multiple where clauses to a Laravel SQL query. My PHP code so far is: date_default_timezone_set('America/Los_Angeles'); $today = getdate(); $year = $today['year']; $month = $today['mon']; $day = $today['mday']; $today_ = $day.

Multiple Where clauses with decryption

Rays Would such a query basically decrypt the username for each where condition or just decrypt it once and apply it to each condition? SELECT * FROM TableA WHERE ( CONCAT_WS( ' ', AES_DECRYPT(TableA.FirstName, "fnkey"), AES_DEC

SQL multiple where clauses

User 5843610 I have a program that can optionally get the Id from three different columns I concatenate all the whereclauses together and then I EXECcommand. Just wanted to see if there is a better way to do this? IF ((ISNULL(@GuidelineIds, '')) <> '') BEGIN

HQL multiple where clauses

gcalex5 I was initially just doing a single variable query and had to add another variable to the statement, can't seem to get it to work or find a direct answer on the expected syntax. I've checked that everything is mapped correctly and no nulls go into the

Firestore: Multiple where clauses

general user let query = db.collectionGroup('volumes') query = query.where('days', 'array-contains', day) query = query.where('start_time', '<', time) query = query.where('end_time', '>', time) snapshot = await query.get().catch(e => consol

HQL multiple where clauses

gcalex5 I was initially just doing a single variable query and had to add another variable to the statement, can't seem to get it to work or find a direct answer on the expected syntax. I've checked that everything is mapped correctly and no nulls go into the

Multiple where clauses, Laravel

Bisebiu I've tried querying with 2 where clauses but the response is not correct, not sure why. $history = AnswerHistory::where('question_id', '=', $id) ->where(function ($query) { $query->where('answer_type', '!=', "skipped"); })

Laravel multiple WHERE clauses

Lakmal Premaratne I need to add multiple where clauses to a Laravel SQL query. My PHP code so far is: date_default_timezone_set('America/Los_Angeles'); $today = getdate(); $year = $today['year']; $month = $today['mon']; $day = $today['mday']; $today_ = $day.

Laravel multiple WHERE clauses

Lakmal Premaratne I need to add multiple where clauses to a Laravel SQL query. My PHP code so far is: date_default_timezone_set('America/Los_Angeles'); $today = getdate(); $year = $today['year']; $month = $today['mon']; $day = $today['mday']; $today_ = $day.

Laravel multiple WHERE clauses

Lakmal Premaratne I need to add multiple where clauses to a Laravel SQL query. My PHP code so far is: date_default_timezone_set('America/Los_Angeles'); $today = getdate(); $year = $today['year']; $month = $today['mon']; $day = $today['mday']; $today_ = $day.

Combining multiple WHERE clauses

user ID I have the following select statement which works for me. select * from BND_listing right join BND_ListingCategories on BND_Listing.CatID = BND_ListingCategories.CatID where (CategoryName = '[querystring:filter-Category]' or '[querystring:

Multiple where clauses, Laravel

Bisebiu I've tried querying with 2 where clauses but the response is not correct, not sure why. $history = AnswerHistory::where('question_id', '=', $id) ->where(function ($query) { $query->where('answer_type', '!=', "skipped"); })

Multiple where clauses, Laravel

Bisebiu I've tried querying with 2 where clauses but the response is not correct, not sure why. $history = AnswerHistory::where('question_id', '=', $id) ->where(function ($query) { $query->where('answer_type', '!=', "skipped"); })

Multiple Where clauses with decryption

Rays Would such a query basically decrypt the username for each where condition or just decrypt it once and apply it to each condition? SELECT * FROM TableA WHERE ( CONCAT_WS( ' ', AES_DECRYPT(TableA.FirstName, "fnkey"), AES_DEC

HQL multiple where clauses

gcalex5 I was initially just doing a single variable query and had to add another variable to the statement, can't seem to get it to work or find a direct answer on the expected syntax. I've checked that everything is mapped correctly and no nulls go into the

Firestore: Multiple conditional where clauses

Pleasure: For example, I have dynamic filters for my book list where I can set specific colors, authors and categories. The filter can be set to multiple colors at once, or multiple categories. Book > Red, Blue > Adventure, Detective. How to conditionally

Multiple where clauses in Rails SearchKick

Jamil Khan I have a very basic question about searchkick. What if you wish to include multiple where statements in your searchkick query using an if statement. much like a query builder @product = Product.all unless request.end_date.nil? @prod

Firestore: Multiple conditional where clauses

Pleasure: For example, I have dynamic filters for my book list where I can set specific colors, authors and categories. The filter can be set to multiple colors at once, or multiple categories. Book > Red, Blue > Adventure, Detective. How to conditionally

Multiple where clauses in Rails SearchKick

Jamil Khan I have a very basic question about searchkick. What if you wish to include multiple where statements in your searchkick query using an if statement. much like a query builder @product = Product.all unless request.end_date.nil? @prod

Multiple WHERE clauses in Firebase query

Special Snowflakes: I'm trying to retrieve data that only returns flight data if the arrival date and airport match. I can't seem to figure out the best solution. I can only extract data at the airport or where the arrival date is the same, but not both (it ca

Multiple where clauses in laravel loop

username I almost want the query to select all user records that are 25 years old and between 150-170cm or 190-200cm. I wrote this query below. However, the problem is that it keeps attracting people who are 25 or 190-200 cm, not 25-year-olds 150-170 or 25 who

SQL RANK with multiple WHERE clauses

Patrick I don't have several sales offices and their salespeople. I'm trying to set up a report that will basically illustrate the performance of each office. It's pretty easy to get some SUM, COUNTs, but I'm having a hard time getting an office rank. I want t

Postgres - Index with multiple where clauses

gozluklu_marti I have a query with two WHERE clauses. It looks like this: SELECT m FROM Media m WHERE m.userid = :id AND m.timestamp = (SELECT MAX(mm.timestamp) FROM Media mm WHERE mm.userid = :id