MYSQL Returning result without empty fields - mysql

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` = ''

Related

Update non-null field with possibly null values

In the query below:
update collect_irc_deploy c
set hid = (select id
from auth_hierarchy
where fqdn = (select location
from reserve
where id=c.rid
)
)
where hid = 0 and rid is not null
the subquery select id from auth_hierarchy where fqdn = (select location from reserve where id = c.rid) may return NULL while the field hid is NOT NULL.
How can I modify the statement so that if the subquery returns NULL that data item is skipped instead of failing the entire execution?
You can use update...join syntax to ensure only joined rows are updated:
update collect_irc_deploy
join reserve on reserve.id = collect_irc_deploy.rid
join auth_hierarchy on auth_hierarchy.fqdn = reserve.location
set collect_irc_deploy.hid = auth_hierarchy.id
where collect_irc_deploy.hid = 0 and collect_irc_deploy.rid is not null
Use UPDATE IGNORE solved my problem. But it will generate warning messages.

Mysql Concat not null value

Guy I'm trying to concat not null value from a list column.
i don't need that null in value
SELECT Emp_fname, Call_number, concat( Aud_name,Mag_name,Boo_name) as name
FROM manage left JOIN call_number ON manage.Man_Call_id = call_number.Call_id
left JOIN book_ ON call_number.Call_id = book_.Boo_id
left JOIN employee_ ON manage.Man_emp_id = employee_.Emp_id
left JOIN audiovisual_ ON call_number.Call_id = audiovisual_.Aud_Call_id
left join magzine_ ON call_number.Call_id = magzine_.Mag_Call_id
not concat
concat
The CONCAT function, if passed even a single NULL value, will just return NULL as a result. If you want to ignore possible NULL values when calling CONCAT with a list of columns, then you may use COALESCE. Here is one version of your query which would completely ignore NULL:
SELECT
Emp_fname,
Call_number,
CONCAT(COALESCE(Aud_name, ''), COALESCE(Mag_name, ''), COALESCE(Boo_name, '')) AS name
FROM manage
...
If you don't want to replace the nulls with empty string, you may use any string replacement you wish.

MYSQL query trim a string based on result of results of another query

I am trying to update a table ONLY if a condition of another table is met. These tables don't have really anything I can join on. (the phppos_app_config is just key/value pairs). The query below does NOT work as I am not allowed to do a count on a subquery.
Here is what I am trying to achieve (doesn't work as it is NOT allowed in sql; but just showing logic)
UPDATE phppos_giftcards SET giftcard_number = TRIM(LEADING ';' FROM giftcard_number)
WHERE
COUNT
(
SELECT value FROM phppos_app_config
WHERE `key` = 'disable_giftcard_detection' and `value` = '0'
) =1
I don't have mysql to test so just let me know if it does not work and I will delete
UPDATE phppos_giftcards SET giftcard_number = TRIM(LEADING ';' FROM giftcard_number)
WHERE ( SELECT count(*) FROM phppos_app_config
WHERE `key` = 'disable_giftcard_detection' and `value` = '0'
) = 1
The use of the Exists Clause in the subquery will simplify the sub query and will perform better as unnecessary comparisons will be avoided (assuming that you want to update if there us at least one record in the PHPOS_APP_CONFIG table with the specified key, value)
UPDATE phppos_giftcards SET giftcard_number = TRIM(LEADING ';' FROM giftcard_number)
WHERE exists
( SELECT count(*) FROM phppos_app_config
WHERE `key` = 'disable_giftcard_detection' and `value` = '0'
)

SQL Query, "[...] WHERE field IS NOT NULL" shows 'field' with NULL values

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

Query not working when looking for null values on the where clause

I got this query that works perfectly:
SELECT FOLDER.folderid, FOLDER.foldername
FROM FOLDER
INNER JOIN COLLABORATORS ON COLLABORATORS.folderid = FOLDER.folderid
WHERE COLLABORATORS.userid = 23
But when I added this on the where clause: "and FOLDER.parent = NULL" it doesnt work. The complete query is:
SELECT FOLDER.folderid, FOLDER.foldername
FROM FOLDER
INNER JOIN COLLABORATORS ON COLLABORATORS.folderid = FOLDER.folderid
WHERE COLLABORATORS.userid = 23 and FOLDER.parent = NULL
Does anyone know whats going on? Thanks
when dealing with NULL, you cannot use = or <> since the values are unknown but instead use IS NULL or IS NOT NULL,
WHERE COLLABORATORS.userid = 23 and FOLDER.parent IS NULL
MySQL - Working with NULL Values
Change this query
WHERE FOLDER.parent = NULL
to
WHERE FOLDER.parent IS NULL
Because the result of any arithmetic comparison with NULL is also NULL, you cannot obtain any meaningful results from such comparisons.
MySQL Manual
For fetching Null Records from a table, it is required to use where [column] is null.
the condition specified in your query FOLDER.parent = NULL, will look for 'NULL' String(word) in the required column.
you can check this by inserting 'NULL' string(word) in the above column.