UNION query problem - mysql

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)

Related

check comma separated field against comma separated value in mysql

A table field has values has 1,2,3 (comma seperated value) and a variable has 2,3. Now i need to check weather variable value is in table field using query along with another name field
user table
id name cat_id
-----------------
1 test 1,2,3
2 test1 3,4
3 test2 4
variable $value = 2, 3
Query : select * from user where name='test' and cat_id IN ('".$value."')
but for above query i get zero data
How to check if given id is exist in cat_id field and name exist in table
You can use a regex to check whether the value is contained in cat_id:
SELECT * FROM user WHERE name='test' AND cat_id REGEXP CONCAT('[[:<:]]', value, '[[:>:]]')
this will attempt to match value at any word boundary in cat_id, so for cat_id='1,2,3', values of (for example) '1,2', '2', '2,3' will match.
To put it in a string form (e.g. for PHP):
$sql = "SELECT * FROM user WHERE name='test' AND cat_id REGEXP CONCAT('[[:<:]]','" . $value. "', '[[:>:]]')";
Your cat_id field should contain only one id by row.
It's normal that your SQL request doesn't work currently because you're looking for cat_id 2 or 3 which SQL is not finding.
For example in your first row 1,2,3, for MySQL it's a string "1,2,3" and not an array of three ids.
If a name can be used by several cats maybe they should be the ones having a name_id.
And if a cat can have several names, and a name can have several cats, you should create a new table cats_names containing one name_id and one cat_id by row.

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.

Select query - auto populate empty field

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

How to select all columns in sql except one column? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL exclude a column using SELECT * [except columnA] FROM tableA?
Is selecting all columns except one column possible??
here is all column names: id, name, address, age
SELECT id, name, address from TBLUser
I don't want to use this select statement because the number of columns of my tables are different to each other.
declare #cols varchar(max), #sql varchar(max)
SELECT #cols = STUFF
(
(
SELECT DISTINCT '], [' + name
FROM sys.columns
where object_id = (
select top 1 object_id from sys.objects
where name = 'TBLUser'
)
and name not in ('age')
FOR XML PATH('')
), 1, 2, ''
) + ']'
select #sql = 'select ' + #cols + ' from TBLUser'
exec (#sql)
How about:
SELECT * FROM sys.columns
WHERE Name <> N'Column To Exclude' and Object_ID = Object_ID(N'TBLUser')
This will return all columns except the one you wish to exclude.
Expanded explanation:
According to this explanation from SQL Cheat Sheet
sys.columns is a system table and is used for maintaining information on columns in a database. For every column added in database, a record is created in the sys.columns table. There is only one record for each column
Name: The name of the column. This is unique within the table object.
Object_id:object_id is unique identifier for table in which the column exists. We will use this column to join sys.columns with sys.tables in order to fetch columns in different tables.
We are selecting all results from sys.columns where the name is not equal to whatever you provide, replace 'Column To Exclude' with your column name. We are also requiring that the object_id equal the object_id you provide. object_id is a number that represents the table you want to filter out the one column from. In this ops case the table was TBLUser, in other uses it would be like this object_id(N'[dbo].[YourTable]'), you would replace [YourTable] with your own table name
in sql*plus,
the one way is to disable as follows:
sql> column age noprint
sql> SELECT * from TBLUser
then, you can revert using
sql>column age off
or else you've to user dynamically with DBMS_SQL package.

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