MySQL (OSX) LEFT JOIN not working as expected - mysql

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.

Related

Unable to create a temporary table from a select query with multiple joins. Throws 'Duplicate column name 'id'

This is the query that is causing the problem:
CREATE TEMPORARY TABLE fetchedCropVariety ENGINE = MEMORY
SELECT *
, hz.zoneName
, i.fullImageUrl
, i.previewImageUrl
, u.userName
FROM seedrecord s
LEFT
JOIN HardinessZone hz
ON hz.id = s.hardiness_zone_id
JOIN image i
ON s.id = i.seedrecord_id
JOIN members u
ON s.FK_USER = u.id
WHERE s.id = 1
AND s.deleted = FALSE;
When i execute this query it throws the error Error Code: 1060. Duplicate column name 'id'
There are no duplicate columns named id in the table seedrecord so this is caused by one or more id columns from the other tables that are only used to join them in order to retrieve zoneName and fullImageUrl.
When i remove *, from the query the query does run but all the fields from seedrecord are omitted. It must be the id column from seedrecord that is clashing somehow with the id columns from hardinessZone and members but I am not trying to include the id fields of those 2 tables into the temporary table i want to create so this error is really puzzeling me.
Can anyone tell me what i can do to stop this error from occurring?
Thank you
don't make
select *
It will take all clomuns that you have in your From Clause
And you have the at least two Columns that are namend id
MAke instead
S.*,u.*
and so on

How to get left table values only (left join)

I have two tables:
bloodrequest (id,name,Rphone,bloodtype) as:
reacheduser (id,Rphonw,Dphone) as:
I want to read all data in bloodrequest if bloodrequest.id is not equal to reachedusers.id where reachedusers.Dphone!="618159985"
I have tried left join , subquery but unfortunately nothing work
hear is some code i tried:
SELECT bloodrequest.id, bloodrequest.magac, bloodrequest.bloodType
FROM bloodrequest
LEFT JOIN reachedusers on bloodrequest.id != reachedusers.id
WHERE reachedusers.Dphone != "618159985"
i want to check if request id in table bloodrequest is in reached table where Dphone ="618159985"
if the id is exist but but dphone is not 618159985 i want to read otherwise not
You can use NOT IN (subquery) to find the rows you want, as in:
SELECT
id, magac, bloodType
FROM bloodrequest
WHERE id NOT IN (
SELECT id
FROM reachedusers
WHERE Dphone = "618159985"
)

return values of table 1 based on single column in table 2

I have 3 tables that I am using and need to make a query to return data from one table based on the value of a single column in the second table.
tbl_user
ID
login
pass
active
mscID
tbl_master
ID
name
training_date
MSCUnit
Active
tbl_msc
mscID
mscName
my current SQL statement:
SELECT
tbl_master.ID,
tbl_master.name,
tbl_master.training_date,
tbl_master.MSCUnit,
tbl_master.active,
tbl_user.mscID
FROM
tbl_master,
tbl_user
WHERE
tbl_master.active = 1 AND tbl_master.MSCUnit = tbl_user.mscID
The values stored in tbl_msc.mscID is a varchar(11) and it contains a string similar to A00 or A19. This is also the Primary key in the table.
The values stored in tbl_user.mscID matches that of tbl_msc.mscID. The values stored in tbl_master.UnitMSC also matches that of tbl_msc.mscID.
My goal is to return all records from tbl_master where the currently logged in user has the same mscID. The problem I am having is the statement returns all records in tbl_master.
I have tried several different join statements and for some reason, I cannot get this to filter correctly.
I am missing something. Any assistance in the SQL statement would be appreciated.
Thanks,
Will
You should be writing this using joins. I don't know how you know who the current user is, but the idea is to join the three tables together:
SELECT m.ID, m.name, m.training_date, m.MSCUnit, m.active,
u.mscID
FROM tbl_master m JOIN
tbl_user u
ON m.MSCUnit = u.mscID JOIN
tbl_msc msc
ON msc.mscID = u.msc_ID
WHERE m.active = 1 AND msc.mscName = ?;
Notice the use of proper, explicit, standard JOIN syntax and table aliases.
Select a.*, b.userid from
table_master a, table_user b where
a.mscunit in (select mscid from
table_user where active=1)
This should point you in the right direction.

Sql query - Select elements such that a condition is not met

I have this query in sqlite:
SELECT
'L_MEDIA_ARTIST'.'MEDIA_ID'
FROM \
'L_MEDIA_ARTIST',
'L_ARTIST_CAT',
'ARTIST_CAT'
WHERE
'L_ARTIST_CAT'.'ART_ID' == 'L_MEDIA_ARTIST'.'ART_ID'
AND
'L_ARTIST_CAT'.'ART_CAT_ID' == 'ARTIST_CAT'.'ID'
AND
('ARTIST_CAT'.'NAME' == 'SINGER' OR 'ARTIST_CAT'.'NAME' == 'ACTOR')
which just selects all the media id such that the artist has at least one of the tag 'SINGER' or 'ACTOR'.
How can I change this query in order to obtain the list of all media such that the actor has neither the tag 'SINGER' nor the tag 'ACTOR'?
The involved tables are built up has follows:
CREATE TABLE 'L_MEDIA_ARTIST' (
'MEDIA_ID' INTEGER,
'ART_ID' INTEGER,
FOREIGN KEY('MEDIA_ID') REFERENCES MEDIA('ID'),
FOREIGN KEY('ART_ID') REFERENCES ARTIST('ID'),
UNIQUE('MEDIA_ID', 'ART_ID'));
CREATE TABLE 'L_ARTIST_CAT' (
'ART_ID' INTEGER,
'ART_CAT_ID' INTEGER,
FOREIGN KEY('ART_ID') REFERENCES ARTIST('ID'),
FOREIGN KEY('ART_CAT_ID') REFERENCES ARTIST_CAT('ID'),
UNIQUE('ART_ID', 'ART_CAT_ID'));
CREATE TABLE 'ARTIST_CAT' (
'ID' INTEGER PRIMARY KEY,
'NAME' TEXT NOT NULL UNIQUE);
You need an aggregation query for this, because you have to check that none of the values for a media are in the list. Just looking on one row doesn't provide enough information:
SELECT l.MEDIA_ID
FROM L_MEDIA_ARTIST l JOIN
L_ARTIST_CAT ac
ON l.ART_ID = ac.ART_ID JOIN
ARTIST_CAT c
ON ac.ART_CAT_ID = c.ID
GROUP BY l.MEDIA_ID
HAVING SUM(CASE WHEN c.Name IN ('SINGER', 'ACTOR') THEN 1 ELSE 0 END) = 0;
Note that I also fixed the query:
Introduced proper join syntax. You should learn modern join syntax.
Added table aliases so the query is easier to write and to read.
Removed the single quotes around table and column names, which just cause syntax errors.
The HAVING clause counts the number of times that "SINGER" and "ACTOR" are found in the data. The = 0 ensures there are none for a given media.
The media IDs that you do not want can be retrieved with this query:
SELECT L_Media_Artist.Media_ID
FROM L_Media_Artist
JOIN L_Artist_Cat USING (Art_ID)
JOIN Artist_Cat ON L_Artist_Cat.Art_Cat_ID = Artist_Cat.ID
WHERE Artist_Cat.Name IN ('SINGER', 'ACTOR')
(This is the same as your first query.)
So you want all media that are not one of those:
SELECT ID
FROM Media
WHERE ID NOT IN (SELECT L_Media_Artist.Media_ID
FROM L_Media_Artist
JOIN L_Artist_Cat USING (Art_ID)
JOIN Artist_Cat ON L_Artist_Cat.Art_Cat_ID = Artist_Cat.ID
WHERE Artist_Cat.Name IN ('SINGER', 'ACTOR'))

Unknown column 'wp_cons_users.id' in 'on clause'

I have three table one is for users and other one is for subject and third one contain user_id, subject_id foreign keys.
I get unknow coloumn when I run the following sql.
SELECT wp_cons_users.first_name, wp_cons_subject.subject, wp_cons_skilllist.skill_level
FROM `wp_cons_subject`
JOIN wp_cons_skilllist ON wp_cons_skilllist.user_id = wp_cons_users.id
JOIN wp_cons_users ON wp_cons_users.id = wp_cons_skilllist.user_id
WHERE wp_cons_subject.id = '1'
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
I can't find the error with this query.
wp_cons_skilllist
column link to
id (primay)
user_id wp_cons_users -> id
subj_id wp_cons_subject -> id
skill_level
Here I try to get the username, skill level and subject for any given subject id.
Looks like your main problem is with the ordering of your JOINs. In your first join, you are matching with wp_cons_users.id, but you don't join that table until later in the query. If you re-order the joins it should work better. Also, based on your table description, it seems that you will also need to join on subject_id. This query should help:
SELECT wp_cons_users.first_name
, wp_cons_subject.subject
, wp_cons_skilllist.skill_level
FROM wp_cons_users
JOIN `wp_cons_subject`
ON wp_cons_users.id=`wp_cons_subject`.user_id
AND wp_cons_subject.id = '1'
JOIN
wp_cons_skilllist
ON wp_cons_skilllist.user_id = wp_cons_users.id
AND wp_cons_skilllist.subject_id = `wp_cons_subject`.id
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
I am guessing about the field names that weren't in your original query, so you may have to make some changes if they're different from what I'm assuming.
Without information about your attributes in your table, I'm afraid we can only assume that there is no ID column in your wp_cons_users table.
when I corrected the query to following it started to work.
SELECT wp_cons_users.first_name, wp_cons_subject.subject, wp_cons_skilllist.skill_level
FROM `wp_cons_skilllist`
JOIN wp_cons_subject ON wp_cons_subject.id = wp_cons_skilllist.subject_id
JOIN wp_cons_users ON wp_cons_users.id = wp_cons_skilllist.user_id
WHERE wp_cons_skilllist.subject_id = '1'
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
LIMIT 0 , 30