SQL Union : Selecting id of file_up table - mysql

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

Related

mysql union of two tables and order the results

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

MySQL: Select all rows from select distinct

I would like to do a specific query
select distinct name
from tablename
to remove duplicates. But this gives me only the names. From 'select distinct name from table' I would like all columns returned with one where condition:
select *
from tablename
where value= 1
I tried this:
select *
from tablename
where value = 1 and exists (select distinct name
from tablename)
Unfortunately it returns the same data as:
select *
from tablename
where value = 1
Which means that there is a fundamental flaw in my query.
Could someone help me with my query. Thank you in advance.
Can you try this select field1, field2, field3 from tablename group by field2
This query will give you the id where you have duplicates:
select distinct id
from table1
group by id
having count(id)>1
and you can put
having count(id)=1
if you want to know the ones that don have duplicates

Need a field in SELECT DISTINCT but I do not want it to be printed

I need to have the ID field in the SELECT DISTINCT in order to differentiate 2 cases: duplicates from not duplicates but namesake.
In other words you may have the same person duplicated many times and people with same name and surname in the same db.
If I do not place the ID field in the SELECT, the query returns duplicates and namesakes.
I have to place the ID to eliminate duplicates only. But at the same time, I would like not to print the ID. IS this possible without using the group by ID?
SELECT DISTINCT ID, Name, Surname
FROM (SUBQUERY THAT RETURNS DUPLICATES)
Sure:
Select c.Name, c.Surname
From (
SELECT DISTINCT ID, Name, Surname
FROM (SUBQUERY THAT RETURNS DUPLICATES)
) as c;
A simple way a select wrapper
select Name, Surname from (
SELECT DISTINCT
ID
, Name
, Surname
FROM (SUBQUERY THAT RETURNS DUPLICATES) ) T

can a table be selected in an sql query to group the result

hello i just wanted to know is it possible to select the table name in an sql query as part of your result
so for example when using UNION to join 2 tables can you specify where each result is coming from because so far i have had to add an extra column called type to each table to specify and it just seems there could be a better way
SELECT id, name, type
FROM table1
UNION ALL
SELECT id, name, type
FROM table2
LIMIT 20`
result =
id, name, type
id, name, type
but i want to still have the type/table name without selecting it or even having it in my table
pleas let me know if this is possible or if this is the only way to do it ether way thanks in advance
Try this:
SELECT id, name, type, 'table1' AS FromWhichTable
FROM table1
UNION ALL
SELECT id, name, type, 'table2'
FROM table2
LIMIT 20

COUNT() and SELECT inside a more complex query

I'm currently using the following query (simplified indeed) :
SELECT
(
SELECT COUNT(id)
FROM bla_1
WHERE id NOT IN (SELECT id FROM blahhh)
)
+
(
SELECT COUNT(id)
FROM bla_2
WHERE id NOT IN (SELECT id FROM blahhh)
)
as count
Before somebody mentions it, may i add bla_1 and bla_2 don't contain the same data, even if with that simplified query it might seem so.
The problem here is that some ids counted by the second query are already taken care of by the first one. In other words, the second query could return '2', and one of those 2 counted rows would already be counted by the first query.
So, since both queries have some ids in common that i don't want to count twice, i came up with that :
SELECT
(
SELECT COUNT(id)
FROM bla_1
WHERE id NOT IN (SELECT id FROM blahhh)
)
+
(
SELECT COUNT(id)
FROM bla_2
WHERE id NOT IN (SELECT id FROM blahhh)
AND id NOT IN (SELECT id
FROM bla_1
WHERE id NOT IN (SELECT id FROM blahhh)
)
)
as count
You will notice that the second subquery inside the second query is the exact same query as the first one.
My problem is that i can't get to make this work without executing the same query twice (a first time to count results, and a second time to actually retrieve those results).
Much love to the one solving that problem :-)
Try using the UNION operation that will eliminate duplicates for you.
SELECT COUNT(ID) AS MyCount
FROM
( SELECT ID FROM Table1 WHERE /*....*/
UNION
SELECT ID FROM Table2 WHERE /*....*/
) r