Filter Left Join Unique Value - mysql

I have two tables and need to choose only the unique value from the left join.
This is the code I tested with.
First table - answered (Field Name - src)
Second Table - noanswered (Field Name - src2)
SELECT DISTINCT src2
FROM answerd
RIGHT noanswered ON answerd.src = noanswered.src2
WHERE answerd.src IS Not NULL;

SELECT DISTINCT *
FROM answered
WHERE NOT EXISTS ( SELECT NULL
FROM noanswered
WHERE answered.src = noanswered.src2 );

Related

fetch id from table1 based on some conditions and check if that id exists in other table

I have 2 tables; alternate identifier table and a master table
the above image represents the column from alternate identifiers table
and master table have these columns
Now, First I fetch company_group_id from alternate identifier table where bank_entity_id='somevalue' and id_value = 'somevalue';
if this query return nothing than end. if this query return company_group_id than I will check that company_group_id in master table where company_group_id = selected company group id.
after getting this if its present in the table than i will check hashcode if it presents in the master table.
separate queries are like this:
select company_group_id from aes_batch.aes_company_group_alternate_identifiers
WHERE ID_VALUE = '525' and BANK_ENTITY_ID='UOBS';
select company_group_id, hashcode from aes_batch.aes_company_group_master where company_group_id = 'value from the last query' and hashcode="value";
I want to combine these queries to get same result.
this is what I tried but failed.
SELECT t1.company_group_id
FROM aes_batch.aes_company_group_alternate_identifiers t1
LEFT JOIN aes_batch.aes_company_group_master t2
ON t1.company_group_id = t2.company_group_id
WHERE t2.company_group_id IS NULL;
SELECT *
FROM aes_batch.aes_company_group_master B
WHERE NOT EXISTS (SELECT 1
FROM aes_batch.aes_company_group_alternate_identifiers A
WHERE B.company_group_id = A.company_group_id);
Select a.company_group_id, b.company_group_id,
b.hashcode, a.id_value
from aes_batch.aes_company_group_alternate_identifiers a
LEFT JOIN aes_batch.aes_company_group_master b
ON b.company_group_id = a.company_group_id
WHERE a.id_value in ('524','525')
and a.bank_entity_id='UOBS';
can someone help me in this?

How to check in MySQL whether the result of one SELECT contains another SELECT?

I have tables
CREATE TABLE one (
op INT,
value INT
);
and
CREATE TABLE two (
tp INT,
value INT
);
Now I want to get all op values for which the set of values for the op contains all values for a given tp.
I would write this as:
SELECT op FROM one AS o1 WHERE (
(SELECT value FROM one AS o2 WHERE o1.op = o2.op)
CONTAINS ALL
(SELECT value FROM two WHERE tp=<specific-value>)
)
Unfortunately, I couldn't find such a CONTAINS ALL operator and nothing which would be close that.
Table one contains 50M entries, table two contains 1M entries. On average, there are 20 different values for a single op and tp.
Consider your tables name ops and tps.
SELECT
ops.op
FROM ops
INNER JOIN tps ON tps.value = ops.value
WHERE tps.tp = 1
GROUP BY ops.op
HAVING COUNT(DISTINCT ops.value) = (SELECT COUNT(DISTINCT tps.value) FROM tps WHERE tps.tp = 1); --- You can replace 1 with any tp value.

mysql left outer join exclude

I have 2 tables 'conta' and 'details' and both the tables have null values and data in different case
conta
id col1 col2 col3 col4
1 Hi Bye See YOU
2 Hello (NULL) (NULL) (NULL)
details
id new_column1 new_column2 new_column3
1 bye see you
I want to apply join based on col2=new_column1 and col3 = new_column2 and col4 = new_column3 and get the values that are present in conta and not in details, so my output will be
conta
id col1 col2 col3 col4
2 hello (NULL) (NULL) (NULL)
But somehow i am unable to do so. I wrote below query, but its simply not resulting me the values i want.
SELECT `id`,`col1`,`col2`,`col3`,`col4` FROM `conta`
WHERE LOWER(`col2`) + LOWER(`col3`) + LOWER(`col4`) NOT IN (SELECT DISTINCT(LOWER(`new_column1`) + LOWER(`new_column2`) + LOWER(`new_column3`))
FROM `details`);
It simply give me no results! in the display
Any help?
Edit: I tried below query as suggested by #Uueerdo and it isn't giving me what i want.
SELECT conta.id,`col1`,`col2`,`col3`,`col4` FROM `conta`
LEFT OUTER JOIN `details`
ON ((conta.col2 IS NULL AND details.new_column1 IS NULL)
OR (LOWER(conta.col2) = LOWER(details.new_column1)))
AND ((conta.col3 IS NULL AND details.new_column2 IS NULL)
OR (LOWER(conta.col3) = LOWER(details.new_column2)))
AND ((conta.col4 IS NULL AND details.new_column3 IS NULL)
OR (LOWER(conta.col4) = LOWER(details.new_column3)))
WHERE details.id IS NULL
In the output in col2 i see a value 'Operations' which is also present in new_column1 in details table. This means it shouldn't be present in the output as i am trying to apply left outer join exclude I even tried using LEFT JOIN instead of LEFT OUTER JOIN and it isn't working either
Edit2: I found the solution. the query works and does the job. Exept that i had to run a command to replace all blank cells in the columns where i am applying join to NULL values.
You're better off using a SELECT .... FROM a LEFT JOIN b ON conditions WHERE b.id IS NULL style of query; null comparisons are a little different (and can be handled join conditions).
For example these evaluate to NULL, which is not true, which is false:
NULL = NULL
NULL IN (NULL)
But you can do things like this to compare nulls more easily:
ISNULL(a, x) = ISNULL(b, x)
(a IS NULL AND b IS NULL)
So you're join condition can be something like:
[...]
ON ((conta.col2 IS NULL AND details.new_column1 IS NULL)
OR (LOWER(conta.col2) = LOWER(details.new_column1)))
AND ((conta.col3 IS NULL AND details.new_column2 IS NULL)
OR (LOWER(conta.col3) = LOWER(details.new_column2)))
[and so on...]
WHERE details.id IS NULL
This assumes details has some sort of non-null row identification field that can used to reliably determine if there was a match or not.
Edit: The precise problem with your current query (aside from the null issues I previously outlined) is that + is not concatenation in MySQL, it is addition. With the data you've shown both LOWER(col2) + LOWER(col3) + LOWER(col4) and LOWER(new_column1) + LOWER(new_column2) + LOWER(new_column3) with yield 0 for the rows without NULL values in them. You would need to use the CONCAT() function to do the operation instead; but I'd discourage it because CONCAT('abc', 'def', '') is equal to CONCAT('ab', 'cd', 'ef').
Sidenote: DISTINCT is not a function, the () will have no effect (other than that they would cause a problem if they contained more than one result field).
You can keep your general format, and the aforementioned null issues, by a simple change with this format : WHERE (a, b, c) IN (SELECT a, b, c FROM ....
You can do a left join, then test for a null in the 2nd table to find the rows in the first table that didn't match any rows in the 2nd table.
SELECT
a.`id`,
a.`col1`,
a.`col2`,
a.`col3`,
a.`col4`
FROM `conta` a
LEFT JOIN `details` b
ON a.`col2` like b.`new_column1`
AND a.`col3` like b.`new_column2`
AND a.`col4` like b.`new_column3`
WHERE b.`id` IS NULL
Your Problem Solution Is inner join your (contact table) with (details table)
and get (contact.*) All columns where Contact.col1 !=details.new_column1
here is a query
Select conta.* from conta inner join details on conta.col1!=details.new_column1
You Can And More Where column in inner join
You could use the EXISTS operator to create an anti-semi join. Please take a look at this link: https://www.techonthenet.com/mysql/exists.php
Example Query:
SELECT
id,
col1,
col2,
col3,
col4
FROM conta
WHERE NOT EXISTS (
SELECT 1
FROM details
WHERE conta.col2 LIKE details.new_column1
AND conta.col3 LIKE details.new_column2
AND conta.col4 LIKE details.new_column3
)

MySQL (OSX) LEFT JOIN not working as expected

Hi: struggling with retrieving only those emails from my marketingImport table that do not exist in my hardBounce table.
I tried various approaches to the LEFT JOIN, but I'm always getting the entire marketingTable (all 300K records). I should be only getting about 220K records, since there are about 80K 'bad' emails in my hardBounce table: those should be excluded from my results.
I also tried replacing WHERE with AND (to make it part of the ON clause), but got same results.
This is my SQL:
SELECT marketingImport.email FROM marketingImport
LEFT JOIN hardBounce ON marketingImport.email = hardBounce.email
WHERE hardBounce.email IS NULL;
Tables:
-marketingImport contains a field 'email' which is a varchar(255), nullable index
-hardBounce contains a single field 'email' which is a varchar(255), nullable UNIQUE index (not PK)
What am I missing? I did read all posts...and my eyes are now watering...
Thank you.
How about using a subquery instead of LEFT JOIN?
SELECT marketingImport.email
FROM marketingImport
WHERE marketingImport.email NOT IN (
SELECT hardBounce.email
FROM hardBounce
);
Try to use NOT EXISTS:
SELECT marketingImport.email FROM marketingImport
WHERE NOT EXISTS (
SELECT 1 FROM hardBounce WHERE hardBounce.email = marketingImport.email
);
And I think there may be null value in hardBounce, so you can get all the the emails from marketingImport.

Right way to do this query

I have a table
form (
int id )
webformsystemflags ( int id, int formid int sysflagid )
sysflag ( int id, name char(10) )
form table is a table which has all the forms
webform is a table which has the forms which have flags applied to it. It has a foreign key formid which is id to the form table and sysflagid which is foreign key to the sys flag table
sys flag is the table which contains the flags. Lets say I have flags defined as 1,2,3
I can have forms which don't have all the flags applied to it, some may have 1, some may have 2 or some may have 3 applied to it or some may have none.
How can I find all the forms which have either flag 1 or flag 2 or flag 3 applied to it ?
This is a common trick to find EXCLUSION. The value I have below of "FlagYouAreExpectingTo_NOT_Exist" is explicitly the one you expect NOT to be there. Here's how it works.
Get every form and LEFT JOIN to the Web System Flags table WITH FINDING the matching form, and flag setting you DO NOT want. If it finds a valid entry for the form and flag, the "formid" in the (wsf) table will exist. So, we want all that DON'T exist, hence the closing WHERE wsf.formid is null.
It will be NULL for those where it is NOT already flagged.
select
f.ID
from
forms f
left join webformsystemflags wsf
on f.id = wsf.formid
AND wsf.sysflagid = FlagYouAreExpectingTo__NOT__Exist
where
wsf.formid is null
You could use a subquery:
SELECT * FROM `form` WHERE `id` IN (SELECT `formid` FROM `webformsystemflags`)
Careful with subqueries on huge databases though. You could do the same thing with joins but this is an easy solution that will get you going.
Or for all results that DO NOT have a certain flag:
SELECT * FROM `form` WHERE `id` IN (SELECT `formid` FROM `webformsystemflags` WHERE `sysflagid` != 1 OR `sysflagid` != 2)
or a join method:
SELECT f.*, r.`sysflagid` FROM `form` f LEFT JOIN `webformsystemflags` r ON r.`formid` = f.`id` WHERE r.`sysflagid` != null
will get you the forms and the related flags. However, it will not get ALL flags in one row if the form has multiple flags on it. That one you may need to do a concat on the flags, but this answer is already growing unnecessarily complex.
*LAST EDIT *
Ok nutsandbolts - You need to update your question cause the two of us have overshot ourselves in a number of different queries and it isn't really helping to come back saying it doesnt give the right results. The right results can easily be reached by simply examining the queries we have provided and using the general logic behind them to compose the query that is right for you.
So my last suggestion - you say you want a query that will return a form IF it has a certain flag applied to it AND that is does NOT have other flags applied to it.
Here it is supposing you wanted all forms with a flag of 1 AND NOT 2 or 3 or none:
SELECT f.*, r.`sysflagid` FROM `form` f LEFT JOIN `webformsystemflags` r ON r.`formid` = f.`id` WHERE r.`sysflagid` =1 AND r.`formid` NOT IN (SELECT `formid` FROM `webformsystemflags` WHERE `sysflagid` = 2 OR `sysflagid` = 3)
Because your webformsystemflags is relational this query will NOT return any forms that do not exist in the webformsystemflags table - so you don't need to consider null.
If this is not what you're looking for I strongly suggest you rewrite your question with absolute and perfect clarity on your needs cause after this one I'm out of this conversation. Much luck to you though. Have fun.
You can use an exists clause to pull records like this:
select a.*
from form a
where exists (select 1
from webformsystemflags
where formid = a.id
and sysflagid IN (1,2,3))
This won't give you the associated flag. If you want that:
select a.*, b.sysflagid
from form a
join (select formid, sysflagid
from webformsystemflags
where sysflagid in (1,2,3)) b
on a.id = b.formid
There are many different ways to solve this.
EDIT: By reading a comment on the other answer it seems the question was unclear. You want the result forms that only have ONE flag? i.e. the form has flag 1 but not 2 or 3?
edit2: if you really just want a true/false query pulling only the true (has a flag):
select a.*, b.sysflagid
from form a
join webformsystemflags b on a.id = b.formid
If you want forms without flags:
select a.*
from form a
left join webformsystemflags b on a.id = b.formid
where b.formid is null
edit3: Based on comment, forms with one flag and not one of the others:
select a.*
from form a
where exists (select 1 from webformsystemflags where formid = a.id and sysflagid = 1)
and (
not exists (select 1 from webformsystemflags where formid = a.id and sysflagid = 2)
or
not exists (select 1 from webformsystemflags where formid = a.id and sysflagid = 3)
)