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
Related
Is there any possible way I can find and set the column name by giving alias
for example
I have a sql queries which contain 4 column name fields. 3 fields are common in all the queries
id, name, field
and there is another field which column name get change every time but the only common thing in that field it has a postfix as __type
so my sql query looks like this
SELECT * from table_name
id, name, field, system_data__value
is there any possible way I can add alias to the name where I found __type as type
so if I run my queries then it look like this
SELECT * from table_name
id, name, field, type
You may use UNION ALL for to set the aliases to the columns posessionally.
You must know some value which cannot present in some column (id = -1 in shown code) for fake row removing.
SELECT *
FROM (
SELECT -1 id, NULL name, NULL field, NULL alias_for_column_4
UNION ALL
SELECT * from table_name -- returns id, name, field, system_data__value
) subquery
WHERE id > 0 -- removes fake row
It is possible that the values in fake subquery needs in explicit CAST() calls for correct datatype of the output columns setting.
I keep searching but I can only find answers on the existence in a table but that is not what I am looking for.
I want to know how I would check the existence of a specific keyword inside the entire database.
I tried selecting from every table but got an error:
SELECT * FROM dvd_drives, compatible WHERE mpn = "700577-1C6"
#1052 - Column 'mpn' in where clause is ambiguous
I can use search inside phpmyadmin but how can I use this in a query?
SELECT EXISTS ( SELECT NULL
FROM table1_name
WHERE column_name = 'value'
UNION ALL
SELECT NULL
FROM table2_name
WHERE column_name = 'value'
UNION ALL
... ) AS check_result;
SELECT EXISTS ( SELECT NULL
FROM table1_name
WHERE column_name = 'value' )
*
EXISTS ( SELECT NULL
FROM table2_name
WHERE column_name = 'value' )
*
... ) AS check_result;
The queries checks the existence only (as required in the question), rows with specified value are not returned.
1st query check does the value is present in at least one of the tables, 2nd checks that it is present in each table at least once.
PS. NULL may be replaced freely with any literal value - zero, one, some string literal, etc...
Just wanted to ask if this is possible or a way to determine the strings that is not on my table. for example
select name from table_person where name IN('name1','name2','name3')
scenario is name1 and name2 is available on my table but what I want to display is name3, since I want to know what are the things I haven't added.
Just playing around with the worst approach (may be).
Not Recommended
SELECT
suppliedArgTable.name
FROM
(
SELECT 'name1' AS name
UNION
SELECT 'name2'
UNION
SELECT 'name3'
) AS suppliedArgTable
LEFT JOIN
table_person TP ON TP.name = suppliedArgTable.name
WHERE TP.name IS NULL;
http://sqlfiddle.com/#!9/edcbe/2/0
NOT IN combined with reversing your query is a solution.
With the 'list' ('name1','name2','name3') in a (temporary) table e.g. temp_list
and with the data in table_person
the query would be:
select name from temp_list
where name not in (
select distinct(name) from table_person
)
distinct removes doubles. (see also MySQL: Select only unique values from a column)
SELECT name_field
FROM (VALUES('name1'),
('name2'),
('name3'),
('name4'),
('name5')) V(name_field)
EXCEPT
SELECT name_field
FROM name_table
You can use a temporary table to hold a list of all names and find non-matching names with EXCEPT.
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.
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