mysql union of two tables and order the results - mysql

Excuse my ignorance. I want to take union of the tables invcount2 and reserve1. What is the error in the below code?
mysql_query("select distinct Name from exam1sem.invcount2 WHERE Name not in
(select Name from exam1sem.invcount2 where Date='$date' AND Time='$time'
union select Name from exam1sem.reserve1 where Date='$date' AND
Time='$time')
union
select distinct Name from exam1sem.reserve1 WHERE Name not in (select Name
from exam1sem.invcount2 where Date='$date' AND Time='$time'
union select Name from exam1sem.reserve1 where Date='$date' AND
Time='$time'
)
order by Avail, TD, NOD
");

Well it seems, You cannot order by something which is not present in select statement when you are using distinct keyword.
You can check this SQL link

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 Union : Selecting id of file_up table

I have two tables treeview_items and file_up
treeview_items
file_up
And a query (not written by me)
SELECT *
FROM treeview_items
UNION
SELECT id + (select max(id) from treeview_items), name,
path as text,dir_id as parent_id
FROM file_up
Now How can I modify this query so that id from file_up table will also be listed in the query result? I have tried few things but still stuck!
Current output and expected output, here column name id from file_up is just for demonstration.
Not sure if i get exactly what you are saying but are you looking for something like this?
SELECT *, WhateverID = NULL
FROM treeview_items
UNION
SELECT id + (select max(id) from treeview_items), name,
path as text,dir_id as parent_id, WhateverID
FROM file_up

using union command on three tables (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.

where not in subquery - SQL Server 2008

The inner query in the following SQL statement is to normalize part of the database (code1, code2, code3, etc.) With the outer query I want to select the codes that aren't in the lookup table (tblicd)
select primarycode from
(
select id, primarycode from myTable
union
select id, secondarycode from myTable
union
select id, tertiarycode from myTable) as t
order by id
where primarycode not in tblicd.icd_id
The query above doesn't run, I'm wondering what I'm doing wrong. The error I get is the multi-part identifier tblicd.icd_id could not be bound
One problem is your ORDER BY and WHERE clauses are reversed. The ORDER BY clause must come after the WHERE clause.
Your WHERE clause is also incorrect. It should be like this:
WHERE primarycode NOT IN (SELECT icd_id FROM tblicd)
ORDER BY id
where primarycode not in tblicd.icd_id
might be
where primarycode not in (SELECT icd_id FROM tblicd )

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