How can I select which table each result is from, when I use a UNION command to search multiple tables?
For example, if there are results from both tables, how can I add a column that will say (or differentiate between) whether it is from tableA or tableB.
try this one, simply add a virtual column for the name of the table.
SELECT *
FROM
(
SELECT *, 'tableA' as tableName FROM tableA
UNION ALL
SELECT *, 'tableB' as tableName FROM tableB
UNION ALL
SELECT *, 'tableC' as tableName FROM tableC
) s
WHERE colName = 'hello'
Related
I wanted to know if it's possible to have one query which I could design so that it would take a certain record from only one table i.e. I have 3 tables and my query, based on a certain parameter would choose one table to take the records from.
You can do this if all tables have equal field names using a UNION query, and by adding a field that specifies from which table the row originated:
PARAMETERS ParamTableName VARCHAR(255);
SELECT * FROM (
SELECT *, "Table1" As TableName FROM Table1
UNION ALL
SELECT *, "Table2" As TableName FROM Table2
UNION ALL
SELECT *, "Table3" As TableName FROM Table3
) TheTables
WHERE TableName = ParamTableName
I have two tables, who have the exact same columns. I want to merge table b into table a and if the dataset has the same ID, I want to use the dataset of table b.
I tried something like:
SELECT *
FROM
((SELECT
*
FROM
tableA) UNION (SELECT
*
FROM
tableB)) AS temp
GROUP BY temp.ID
ORDER BY temp.ID
but that gave me a mix of both tables.
You can do this using union all along with some additional logic:
select b.*
from b
union all
select a.*
from a
where not exists (select 1 from b where b.id = a.id);
How to:
Get values from table1.column1 (e.g. abc)
table1.column1=abc
Concatenate them with certain fixed strings, e.g.
xxx
yyy
zzz
Insert the results as separate rows in table2.column2. The final result should be rows with values like this:
table2.column2=abc_xxx
table2.column2=abc_yyy
table2.column2=abc_zzz
(table2 has a connecting column indicating to which ID the table2.column2 record corresponds in case this matters)
Repeat this process for all records in table1.column1 which have table1.item_id > 100
EDIT: For certain convenience I would like the final result rows sequence to look like:
source1_xxx
source1_yyy
source1_zzz
source2_xxx
source2_yyy
source2_zzz
and not like:
source1_xxx
source2_xxx
source1_yyy
source2_yyy
source1_zzz
source2_zzz
If I understand you correctly, you want N (e.g. 3) entries for every existing row in Table1. If so, you can CROSS JOIN Table1 to a projection of the values, like so:
INSERT INTO Table2(column2)
SELECT CONCAT(t1.column1, '_', x.col)
FROM Table1 t1
CROSS JOIN
(SELECT 'xxx' AS col
UNION
SELECT 'yyy'
UNION
SELECT 'zzz') x;
SqlFiddle here
Edit
The query was updated to be cognaisant of the ordering and filtering requirements as well:
INSERT INTO Table2(column2)
SELECT CONCAT(t1.column1, '_', x.col)
FROM Table1 t1
CROSS JOIN
(SELECT 'xxx' AS col
UNION
SELECT 'yyy'
UNION
SELECT 'zzz') x
WHERE t1.ID > 100
ORDER BY t1.column1 ASC, x.col ASC;
With an updated SqlFiddle
Modifying the answer.
Credit goes to the StuartLC ..he is right, you would need to use cross join
INSERT INTO Table2(column2)
SELECT CONCAT(t1.column1, '_', x.col)
FROM Table1 t1
CROSS JOIN
(SELECT 'xxx' AS col
UNION
SELECT 'yyy'
UNION
SELECT 'zzz') x;
is this what u want
insert into table2(column2)
select concat(col1,'_','xxx') from table1
union
select concat(1col1,'_','yyy') from table1
union
select concat(col1,'_','zzz') from table1
else keep this entire select statemnts in aview and use it in the insert statement
create view abc
as
select concat(col1,'_','xxx') from table1
union
select concat(1col1,'_','yyy') from table1
union
select concat(col1,'_','zzz') from table1
then
insert into table2(column2) select * from abc
I'm making this select:
select code,name from talbe1
union all
select code,name from table2
The actual code isnt important to me but what is important to me is that the code column will be unique column, and with this select I cant grantee it..
Is there any save word/something that will give me something like that:
select getUniqueCode(),name from(
select name from talbe1
union all
select name from table2)
Thanks.
Have a look at the mysql UUID call. Which would result in something like this:
select UUID(),name from(
select name from talbe1
union all
select name from table2)
remove "all":
select code,name from table1
union
select code,name from table2
Union all keeps al rows.
Union removes duplicates.
If you have different names for the same code in each table, you have to pick one name - try this:
select code, max(name) as name
from (select code,name from table1
union
select code,name from table2) x
group by 1
I have two tables a and b which has a field name in it.
I need to list the data from these two tables. I thought of using union but in the result list data from the first table appears and then followed by the second.
what i want is to order by the field name so the result should be a mixed up of two tables in the order of name that is order by name.
select slug, name, 1 as mt
from tablea
union
select slug, name, 0 as mt
from tableb
order
by name;
The above is working well for me. will there be any complications in the result of this?
Suppose your query is
SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1
u can make it a subquery like this
SELECT * FROM (SELECT field1 FROM TABLE1 WHERE 1
UNION SELECT field1 FROM TABLE2 WHERE 1) AS `result` ORDER BY `result`.`field1`
Or, you could use a Join query such as:
SELECT tablea.firstname, tablea.middlename, tablea.lastname, tableb.phone
FROM tablea, tableb
WHERE tablea.ID = tableb.ID
Then, you could sort the result however you like.