using union command on three tables (mysql) - mysql

I have the following:
tblOwner: owner_num,lname,fname (PK owner_num)
tblMarina_slip: owner_num, slip_id (PK slip_id, FK owner_num)
tblService_Requests: service_id, slip_id, description (PK service_id, FK slip_id)
I am new to MySql and need to use a Union command to get an output of
tblOwner.Owner_num, tblOwner.Lname, tblOwner.Fname, tblServiceRequests.service_id, tblServiceRequests.Description
I have no idea how to use a "union" command to get the required dataset, when the only connection between these three tables the foreign keys in tblMarina_slip and tblService_Requests.

You don't need a union comand:
select tow.Owner_num, tow.Lname, tow.Fname, ts.service_id, ts.Description
from tblOwner tow, tblServiceRequests ts, tblMarina_slip tm
where tow.owner_num = tm.owner_num
and tm.slip_id = ts.slip_id

You need a join not a union :
SELECT t.owner_num,t.Lname,t.Fname,s.service_id,s.description
FROM tblOwner t
INNER JOIN tblMarina_slip p on(t.owner_num = p.owner_num)
INNER JOIN tblService_Requests s on(p.slip_id = s.slip_id)
A union is used to combine two or more outputs together (only in case they are the same with the column numbers and column types)
When you want to display more then one table info in the same row, you should use JOIN like in my answer.
EDIT:
If you are required to use union, you can trick the data like this:
SELECT max(owner_num),max(Lname),max(Fname),slip_id,max(description)
FROM
(SELECT owner_num,max(Lname) as Lname,max(Fname) as Fname, max(slip_id) as slip_id,'' as description
FROM(
SELECT owner_num,Lname,Fname,0 as slip_id from tblOwner
UNION
SELECT owner_num,'' as Lname,'' as Fname,slip_id from tblMarina_slip)
GROUP BY owner_num
UNION
SELECT 0 as owner_num, '' as Lname, '' as Fname,slip_id,description FROM tblService_Requests)
GROUP BY slip_id

select col1,col2,col3 from <tablename> where <conditions>
union
select col1,col2,col3 from <tablename> where <conditions>
union
select col1,col2,col3 from <tablename> where <conditions>
Note : col1, col2, col3 from each table must be identical.

Related

SQL search in the same order as requested and showing not found items

I want to do a special query for SQL but I'm not able to get the wanted result.
My basic query is:
select * from TABLE_NAME where COLUMN_NAME_1 in ('ABC1', 'ABC98', 'ABC97', 'ABC2', 'ABC3')
Imagine that the item 'ABC1' is not in the table. I would like my query to show it. In addition I would like that my query shows the items in the same order I asked (ABC1, ABC98, ABC97, and so on).
I attach an image where you'll understand better what I want.
Thanks in advance
You need to use an outer join or union all. One method looks like:
select v.col1, . . .
from (select 'ABC1' as col1, 1 as ord union all
select 'ABC98', 2 union all
. . .
) left join
TABLE_NAME t
on t.column_name_1 = v.col1
order by ord;
The exact syntax for the list of values varies by database. The above works for MySQL. Oracle would require from dual in the subqueries.
First of all you need a table of the values to show along with their desired position. You can create this on the fly in the FROM clause. Then outer join your table:
select keys.name, t.column_name_2, t.column_name_3, t.column_name_4
from
(
select 'ABC1' as name, 1 as sortkey
union all
select 'ABC98' as name, 2 as sortkey
union all
select 'ABC97' as name, 3 as sortkey
union all
select 'ABC2' as name, 4 as sortkey
union all
select 'ABC3' as name, 5 as sortkey
) as keys
left join table_name t on t.column_name_1 = keys.name
order by keys.sortkey;

SQL Join 2 tables with almost same field

I need to join two tables in SQL. There are no common fields. But the one table have a field with the value krin1001 and I need it to be joined with the row in the other table where the value is 1001.
The idea behind the joining is i have multiple customers, but in the one table there customer id is 'krin1001' 'krin1002' and so on, in this table is how much they have sold. In the other table there customer is is '1001' '1002' and so on, and in this table is there name and adress and so on. So it will always be the first 4 charakters i need to strip from the field before matching and joining. It might not always be 'krin' i need it to work with 'khjo1001' also, and it still needs to join on the '1001' value from the other table.
Is that possible?
Hope you can help me.
You need to use substring:
ON SUBSTRING(TableA.Field, 5, 4) = TableB.Field
Or Right:
ON RIGHT(TableA.Field, 4) = TableB.Field
You can also try to use CHARINDEX function for join operation. If value from table1 contains value from table2 row will be included in result set.
;WITH table1 AS(
SELECT 'krin1001' AS val
UNION ALL
SELECT 'xxx'
UNION ALL
SELECT 'xyz123'
),
table2 AS(
SELECT '1001' AS val
UNION ALL
SELECT '12345'
UNION ALL
SELECT '123'
)
SELECT * FROM table1 AS t
JOIN table2 AS T2 ON CHARINDEX(T2.val, T.val) > 0
Use it as:
SELECT
*
FROM table t1
INNER JOIN table t2 ON RIGHT(t1.col1, 4) = t2.col1;

add column to say which table a union result is from

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'

MySQL get unique key while selecting

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

union two tables and order by a common field like name which is present in both the tables

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.