Table Photos-------
photo1 imagem1.jpg
photo2 imagem2.jpg
photo3 empty
photo4 empty
photo5 enpty
I have a table, with 5 columns who represents photos.
So,
I want to select * from table, all columns... but if one or more of them are empty, I don't want to select. is that possible?
For example, if I do this
Select * from table Photos where photo1 is not null, and photo2 is not null, and photo3 is not null and photo4 is not null and photo5 is not null
that is not possible, because if one of them are empty... the select don't give any values.
anyone know another way?
and also, i need that because if I want to do this:
the problem is.... when I use php for example this
if($project['photo5']!='' and $project['photo5']!=null){
(here i show the picture)
}
If the row is empty... php is ignored and show a error image
Concat() will return null if any one of the values concat is null (mySQL DOCS) Since you want to only show records where none are null concat together and check for not null.
Select *
from table Photos
where concat(photo1,photo2,photo3,photo4,photo5) is not null
AND... your approach should work if you used or's instead of AND and possibly a not. I just find the above easier to read.
Related
Hope the question is not too generic. Couldn't find anything on the site or in SQL documentation:
While coding, i tested this, and to my surprise it worked:
SELECT * FROM cal_entry WHERE cal_entry.parent_id <> 'null'
It actually shows the rows without the ones with NULL values (these are real NULL values in database, not strings with 'null' inside).
According to the docs, I should have used NOT NULL, of course. By the way, it doesn't work with = 'null', like it is correctly stated in the docs.
Can someone explain that?
You are selecting all rows where <> 'null' is true.
Comparing(equals or not-equals) to null is null, so if a row where cal_entry.parent_id is null, your condition will be false/null.
So your query gets all rows that are not null, nor contain the string 'null'.
(Note, you could just as well have written <>'something_else')
Assuming parent_id in an int column the query will return all non-null, non-zero rows:
SELECT *
FROM (
SELECT NULL AS parent_id UNION ALL
SELECT 0 UNION ALL
SELECT 1 UNION ALL
SELECT 2
) AS cal_entry
WHERE cal_entry.parent_id <> 'null'
-- returns 1 and 2 but not 0!
When comparing a number to string MySQL will convert the string to number. Some examples:
'null' becomes 0
'asdf' becomes 0
'1asdf' becomes 123
'1' becomes 1
Your query will behave like:
WHERE cal_entry.parent_id <> 0
this operator give you result of not equal to. ex. $var != null.
we write in mysql as <>. this is kind of validation that the value shoud never be equal to null.
When working with null following two statements should always be taken note of -
An expression can be null, but it can never be equal to null.
Two nulls are never equal to each other.
So, in your query wherever there is a comparison null<>null it returns true by second statement.
Also, always account the possibility that some rows might contain null -
Select * from cal_entry where cal_entry.parent_id!=10
This query would leave out the rows with null entries. Instead use -
Select * from cal_entry where cal_entry.parent_id!=10 or cal_entry.parent_id is null
I'm trying to merge 2 tables x and y having the same columns inside both, But I just want to find just 'EMPTY' lines on column name from both tables and add extra column as Source to know from what table the line came from.
The column name is varchar(128), I tried to find by NULL instead of 'EMPTY' and did not work, might be better to find NULL since I add an extra query just to set 'EMPTY' where is NULL
This is what I currently have:
SELECT data,name,id,'Friends' as Source FROM droid_friends union all SELECT data,name,id,'Followers' as Source FROM droid_followers WHERE name = 'EMPTY'
if you want use NULL your query should be:
SELECT data,name,id,'Friends' as Source FROM droid_friends union all SELECT data,name,id,'Followers' as Source FROM droid_followers WHERE name IS NULL
Seems like this is the solution
SELECT * FROM ( (SELECT data,name,id,'Friends' as Source FROM droid_friends WHERE name='EMPTY' )UNION(SELECT data,name,id,'Followers' as Source FROM droid_followers WHERE name='EMPTY' )) as combined
I'm new to SSRS, and have been tasked with a report that allows users to select NULL, non NULL or both.
For example
Name | ID
BOB 1
ALICE 2
DAVE NULL
ROGER NULL
Users need to be able to select Records with an ID value and/or records with no ID value.
So it would be a multi select parameter (ID, NO ID , ALL).
I can make this work with a sql query, but am not sure how to make it work with user parameters that can be either, or or both.
The id field is a decimal.
Any advice for how to do this?
Thanks.
One way would be to include conditions like the following in the WHERE clause:
and (ID is null and 'NO ID' in #Parameter or
ID is not null and 'ID' in #Parameter)
Alternatively, if the non-null ID values are always positive, you could use "NO ID" and "ID" as the parameter value labels, with the actual available values as 0 and 1 respectively, and use a condition like:
and sign(coalesce(ID, 0)) in #Parameter
(These conditions won't work in conventional SQL - where #Parameter will always be single-valued - but should work in SSRS.)
In the general tab of your Report parameter. There is an option for Allow Null Values. Check that box.
Also update your query to
From UserTable
Where (ID IS NULL OR ID IN (#IDparam))
Here are links if you need to know more abut Multi Select parameters
http://jsimonbi.wordpress.com/2011/01/15/using-multi-select-with-stored-procedures/
http://technet.microsoft.com/en-us/library/aa337396(v=sql.105).aspx
I'd like to limit my query to show only rows where a certain field is not empty. I found this thread where someone posed the same question and was told to use IS NOT NULL. I tried that, but I'm still getting rows where the field is empty.
What is the correct way to do this? Is Null the same thing as Empty in SQL/MySQL?
My query, if you're interested is:
SELECT * FROM records WHERE (party_zip='49080' OR party_zip='49078' OR party_zip='49284' ) AND partyfn IS NOT NULL
I got it by using AND (partyfn IS NOT NULL AND partyfn != '')
When comparing a NULL value, the result in most cases becomes NULL and therefor haves the same result as 0 (the FALSE value in MySQL) inside WHERE and HAVING.
In your given example, you don't need to include IS NOT NULL. Instead simply use party_zip IN ('49080', '49078', '49284'). NULL can't be 49080, 49078, 49284 or any other number or string.
What you do need to think about though, is when checking for empty values. !party_zip won't return TRUE/1 if the value is NULL. Instead use OR columns IS NULL or !COALESCE(party_zip, 0)
this is my first question ever, so please be patient.. :)
We are two developers and both have the same MySql DB with same tables and values.
One is MySql version 5.5 and works ok (apparently) as I am told by the other developer.
On my machine with MySql 5.1.44 (a basic MAMP install) I have the following weird problem.
A very huge query (not mine) fails with error "Column 'xd' cannot be null".
Removing pieces I slimmedi it down to this:
select xd, avg(media) from questionario_punteggi where somefield = 1 union select 1,2
Note, there is no record with somefield = 1 so the first select returns an empty set
We have a SELECT with AVG() function that returns an empty set UNION another SELECT that returns something (1,2 are just random values I put now as an example)
If I remove the AVG() the query works.
If I remove xd (and the 2 of 1,2 to the right) the query works.
If I remove the UNION the query works.
If I set some record with somefield = 1 the query works.
On the other machine 5.5 the query works.
Otherwise the error is:
1048 - Column 'xd' cannot be null
Fields are:
`xd` char(3) NOT NULL DEFAULT '001',
`media` decimal(7,4) NOT NULL DEFAULT '0.0000',
`somefield` tinyint(4) NOT NULL DEFAULT '0',
Gosh. Any help? Thanks.
UPDATE
It has been reported to me as a BUG in MySql <= 5.1 that was fixed before MySql 5.5. I don't have the details but I trust the source
I suggest reversing the order of the queries in the UNION.
This is because the first SELECT in a UNION determines the data type of the columns in the resultset; in your case, the first column of the UNION took the type of the questionario_punteggi.xd column: that is, CHAR(3) NOT NULL.
Since you are applying an aggregate function over the first part of the UNION, it results in a single row even though no records are matched by the filter criterion. As documented under GROUP BY (Aggregate) Functions:
AVG() returns NULL if there were no matching rows.
The value taken for the hidden xd column would normally be an indeterminately chosen record from those that match the filter (which is why you probably don't want to do that anyway); however, since in this case no records match, the server instead returns NULL (which obviously cannot go into a column with the NOT NULL attribute).
By reversing the order of the UNION, the column will not have the NOT NULL attribute. You may need to alias your columns appropriately:
SELECT 1 AS xd, 2 AS avg_media
UNION
SELECT xd, AVG(media) FROM questionario_punteggi WHERE somefield = 1
Using this to explain each of your observations in turn:
If I remove the AVG() the query works.
Since aggregation is no longer performed, the first SELECT in the UNION yields an empty recordset and therefore no NULL record in the first column.
If I remove xd (and the 2 of 1,2 to the right) the query works.
Since the hidden column is no longer selected, MySQL no longer returns NULL in its place.
If I remove the UNION the query works.
This is the bug that was likely fixed between your version of MySQL and your colleague's: the NOT NULL attribute shouldn't really apply to the UNION result.
If I set some record with somefield = 1 the query works.
The value selected for the hidden column is an indeterminate (but non-NULL value, due to the column's attributes) from the matching records.
On the other machine 5.5 the query works.
This bug (I'm still searching for it) must have been fixed between your respective versions of MySQL.
try using the SELECT IFNULL();
Select IFNULL(xd,0), avg(media) f
rom questionario_punteggi
where somefield = 1
union
select 1,2
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull