Select query - auto populate empty field - mysql

Is it possible to automatically populate empty or null field with sql query?
For example i have have table with three columns; ID, Name, Letter.
When fetching data with SELECT ID, Name, Letter FROM table i want to fill column Letter with A if it is empty for certain record?

Use coalesce
SELECT ID, Name, coalesce(Letter, 'A') as Letter
FROM your_table
or IFNULL
SELECT ID, Name, ifnull(Letter, 'A') as Letter
FROM your_table

Related

How to concat two row data in mysql if they are the same

I have a mysql table where I some data are repeated in one column but have different value in another. I want to concat them an them and create new string.
I am getting data in following format:
See rows with checkbox option and vodaphoneissues and comments are repeated twice, yet have different values in another column.
I want to concatenate that value with comma separated format.
There is a function called GROUP_CONCAT.
I do not know names of your columns, let's say they are COLUMN1, COLUMN2, etc. The code will be:
SELECT COLUMN3, GROUP_CONCAT(COLUMN4) FROM your_table GROUP BY COLUMN3;
Use GROUP_CONCAT for that purposes
SELECT id, GROUP_CONCAT(DISTINCT column_name ORDER BY column_name SEPARATOR ', ')
FROM table
GROUP BY comments, versions;
Also pay attention to GROUP BY clouse as it groups by few columns:
GROUP BY comments, versions;

mysql find out the non repeated customers

I have a table like this,
test#text.com
test12#text.com
test123#text.com
test12#text.com
test#text.com
test12#text.com
test1#text.com
I want the email which are not repeated such as result will be
test1#text.com
Assuming your table name is sample_table your column name is sample_column.
Try this
select sample_column
from sample_table
group by sample_column
having count(sample_column)=1;
DEMO
You can try this...
SELECT DISTINCT(email_column_name)
from tableName
GROUP BY email_column_name;
and if you want more columns information related row then try this.
SELECT DISTINCT(email_column_name), ID, firstName,LastName
from tableName
GROUP BY email_column_name;
This is incorrect:
SELECT DISTINCT(email_column_name), ID, firstName, LastName
DISTINCT works on all columns selected, it is not a function where you can apply it to a single column in the select. So if an email were to appear twice with a different last name that email would be returned in both cases.

Return rows with more than one comma separated value in a string

I have a table called contacts and in that table there is a field called contact_type.
contact_type is varchar and stores comma separated values in a sting like this:
^Media^,^Historical^
However only a few rows out of thousands have more than one value stored and I need to to run a query that will return only the rows with more than one so if it stores just ^Historical^ then it will be ignored.
I’m pretty much stumped on how to build a query like this. I assume it will contain something like this:
SELECT LENGTH(#css) - LENGTH( REPLACE( #css, ',', '') ) + 1;
Basically you need to select the records where contact_type contains a comma
select * from your_table
where instr(contact_type, ',') > 0

Masking values in SQL select return

This is an odd question, but I was wonder if you could display one column in the results, but really have another column as the values (MYSQL). Suppose I have this table:
ID Name
1 Soccer
2 Football
I was wonder if it was possible to select all IDs from this table (select ID from table), but the results would display the name instead.
Or is it possible to display as the result (as a single column)?:
1 (Soccer)
2 (Football)
SELECT ID, CONCAT("(", name, ")") FROM <TABLENAME>
This will have results concatenated into single column.
SELECT CONCAT(ID, ' (', Name, ')')
FROM tableName
SQLFiddle Demo

UNION query problem

i have the following union query sql in my code
(SELECT TableA.ID, TableB.Group, '' as Name from TableA,TableB
where TableA.Ipfield=TableB.Androfield)
UNION (SELECT TableA.ID,'',TableC.Name where TableA.irgroup=TableC.iqgroup)
The problem is I need to export this sql as csv file, while i export as csv it should show the column names at begiining of file so i had used '' as Name in query1 so as to display Name as one column along with ID and Group. But the issue here is the column names are displayed properly but the datas are displayed twice , one with Name as '' and another with value for Name.
Name is a field in TableC and not in tableA and tableB.
Is there any way i can display the datas only once with value for Name, I dont need the result with Name as '', it is just used to display the column name as Name along with ID and Group
Thanks Kindly Help!
just union one row with column names firts:
select 'ID' as ID, 'Group' as Group, 'Name' as Name from dual
union
(and here goes the rest of your query)