I must be doing something wrong, or I don't understand the "IS NOT NULL" part, should it be showing me rows with NULL columns that I specifically wanted to no be NULL?
You are doing a left outer join on guesses. When the condition in the on is false, no row will be returned for that join and the fields referenced from that table will be null.
You have a few options I think, all depending on your needs:
Put this condition in the where clause;
Don't use the left outer join, but just a regular join;
Use coalesce to default the values.
Try use the below constructed query
select * from Tbl_EmployeeDetails where (name is not NULL or name <> 'null')
Just user regular join instead of left outer join :
SELECT guesses.*, games.* FROM games,guesses
WHERE guesses.game_id = games.game_id
AND games.real_score_team_1 IS NOT NULL AND games.real_score_team_2 IS NOT NULL ;
That should do the trick
Related
I want to join below tables in such a way that that it should return matched value if not matching it should return a value as "NA".
below are the table details
available tables:
desired output:
Use Left Join between the tables, to consider those rows also which do not exist in the Process table.
You can use Ifnull() function, to set the value NA if no matching row (thus null value) in the Process table.
Try the following (change table and column name(s) accordingly):
SELECT r.Ticket_id,
r.Status,
r.Department,
r.Owner,
r.Process_id,
IFNULL(p.Proces_Name, 'NA')
FROM Resolution AS r
LEFT JOIN Process AS p ON p.Process_id = r.Process_id
If a value isn't found in a MySQL LEFT JOIN, the field is "filled" with NULL
The best practice, as I understand your question is IFNULL
SELECT
r.*,
IFNULL(process_name,'NA')
FROM
resolution r
LEFT JOIN processes p ON p.process_id = r.process_id
select a.ticket_id,a.status,a.department,a.ownername,
case
when b.process_name is null then 'NA'
else b.process_name
end as proces_name
from resolution_tab a
left join process_tab b
on a.process_id=b.process_id
order by a.ticket_id;
I have two tables
Table 1 : assetdetail : itm_id,itm_slno,itm_desc,..
Table 2 : itmtype : typ_id,typname,..
Table2 Contains 6 different typname.
But when I use right join it returns only 4 records.
My query is :
SELECT a.itm_id,i.typname,a.itm_slno,a.itm_desc,a.itm_class from assetdetail
a right join itmtype i on i.typ_id=a.typ_id where a.itm_id!='234'
and itm_class='XYZ' group by i.typname
I need to get all the 6 typename records in the join query.What is the wrong with my query ?
In a right join, conditions on the first table need to go into the on clause, not the where clause:
SELECT a.itm_id, i.typname, a.itm_slno, a.itm_desc, a.itm_class
from assetdetail a right join
itmtype i
on i.typ_id = a.typ_id and a.itm_id <> '234' and a.itm_class = 'XYZ'
group by i.typname;
Non-matching rows in the first table have NULL values, which fail the comparisons in the where.
I would write this as a left join, however. Most people find the logic easier to follow: keep all rows in the first table, regardless of whether there is a match:
SELECT a.itm_id, i.typname, a.itm_slno, a.itm_desc, a.itm_class
from itmtype i left join
assetdetail a
on i.typ_id = a.typ_id and a.itm_id <> '234' and a.itm_class = 'XYZ'
group by i.typname;
The conditions remain the same.
The following code below gives me a result of
but how do I make it so that I get a result of
Code:
SELECT * FROM `shop_shipping_rules` LEFT JOIN `countries` ON `shop_shipping_rules`.shop_shipping_rule_country_iso = `countries`.iso
LEFT JOIN `shop_shipping_regions` ON `shop_shipping_regions`.shop_shipping_rule_region_code = `shop_shipping_rules`.shop_shipping_rule_region_code
WHERE `website_id` = 64 AND `shop_shipping_rule_name` IS NOT NULL
In your predicate, filter out empty fields.
AND `shop_shipping_rule_name` IS NOT NULL AND `shop_shipping_rule_name` <> ''
It's also best practice to stay away from SELECT * and use a column list. Also, you should use aliases and be explicit with your columns.
You will probably want to go through these and make the empty values NULL:
UPDATE `shop_shipping_rules`
SET `shop_shipping_rule_name` = NULL
WHERE `shop_shipping_rule_name` = ''
In mysql I have tableA with rows userid and valueA and tableB with userid and valueB.
Now I want all entries from tableA which don't have an entry in tableB with the same userid.
I tried several things but can't figure out what I do wrong.
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
This is a very good start actually. It gives me all entries from tableA + the corresponding values from tableB. If they don't exist they are displayed as NULL (in phpmyadmin).
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
where `tableB`.`valueB` = NULL
Too bad, empty result. Maybe this would have been too easy. (By the way: tableA has ~10k entries and tableB has ~7k entries with userid being unique in each. No way the result would be empty if it would do what I want it to do)
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
where `tableA`.`userid` != `tableB`.`userid`
This doesn't work either, and to be honest it also looks totally paradox. Anyways, I'm clueless now. Why didn't my 2nd query work and what is a correct solution?
You are almost there. That second query is SO close! All it needs is one little tweak:
Instead of "= NULL" you need an "IS NULL" in the predicate.
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
where `tableB`.`valueB` IS NULL
^^
Note that the equality comparison operator = will return NULL (rather than TRUE or FALSE) when one side (or both sides) of the comparison are NULL. (In terms of relational databases and SQL, boolean logic has three values, rather than two: TRUE, FALSE and NULL.)
BTW... the pattern in your query, the outer join with the test for the NULL on the outer joined table) is commonly referred to as an "anti-join" pattern. The usual pattern is to test the same column (or columns) that were referred to in the JOIN condition, or a column that has a NOT NULL constraint, to avoid ambiguous results. (for example, what if 'ValueB' can have a NULL value, and we did match a row. Nothing wrong with that at all, it just depends on whether you want that row returned or not.)
If you are looking for rows in tableA that do NOT have a matching row in tableB, we'd generally do this:
SELECT * FROM `tableA`
left join `tableB` on `tableA`.`userid` = `tableB`.`userid`
where `tableB`.`userid` IS NULL
^^^^^^ ^^
Note that the IS NULL test is on the userid column, which is guaranteed to be "not null" if a matching rows was found. (If the column had been NULL, the row would not have satisfied the equality test in the JOIN predicate.
Change = NULL for IS NULL on your code. You can also use NOT EXISTS instead:
SELECT *
FROM `tableA` A
WHERE NOT EXISTS (SELECT 1 FROM `tableB`
WHERE `userid` = A.`userid`)
this will help explain the use of working with NULL values:
http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html
the less expensive query would be this, all the other queries sugested are too expensive if the database is too big:
SELECT * FROM tableA a
WHERE a.userid NOT IN
(SELECT b.user_id FROM tableB b);
I would like to filter the highest person of each sex in user table, this is the code that works fine:
SELECT s1.name, s1.sex, s1.height
FROM user s1
LEFT JOIN user s2 ON s1.sex =s2.sex AND s1.height < s2.height
WHERE s2.name IS NULL;
But this one won't work:
SELECT s1.name, s1.sex, s1.height
FROM user s1
LEFT JOIN user s2 ON s1.sex =s2.sex AND s1.height < s2.height AND s2.name IS NULL;
Is that mean 'is null' can only be used after 'where' clause?
The first version is excluding the records that do not have a matching row in the user table -- these will have a null value when you join.
The second version is filtering records where the value in the name column is null.
Visual Explanation of Joins
No.
Take a look on your second query, s2.name is not yet NULL because you are still joining the rows. Thus, the resulting value of the column can be evaluated as null or null not after it has been joined and can be filtered on the where clause.