display both queries and displaying all results into one - mysql

i am trying to display both queries and displaying all results into one
this is what i want to achieve
this is the query i used, but union takes the common ones only, but i want both from query 1 and query 2.
select * from demo a where ( name, dob ) in ( select name,dob from demo group by name, dob having count(*) > 1 )
UNION
select * from demo a where ( name, mname) in ( select name, mname from demo group by name, mname having count(*) > 1 )
order by name ASC

Related

Is it possible to output the result of a union of two subqueries in alternating order?

Lets say I have two subqueries in a UNION statement like so:
(
SELECT *
FROM users
ORDER BY registration_date
)
UNION ALL
(
SELECT *
FROM food
ORDER BY popularity
)
The output is the following:
Bob
Alice
Steve
Mark
...
Sandwich
Pizza
Burger
Fries
...
Is it possible to output them in an alternating fashion, such that the output is:
Bob
Sandwich
Alice
Pizza
Steve
Burger
Mark
Fries
...
Each query output is thousands of items.
You could use row_number() if you are running MySQL 8.0:
(select name, 1 src, row_number() over(order by registration_date) rn from users)
union all
(select name, 2, row_number() over(order by popularity) from food)
order by rn, src
In each unioned subquery, we use row_number() to rank the records, and add another column, called src to identify from which table the record comes from.
Then all that is left to do is order by the assigned row_number(), using the additional column to alternate the records.
Note that I modified your query to enumerate the columns being selected in the subqueries; select * is generally not a good practice, especially with union all, which requires both datasets to have the same number of columns (with equivalent datatypes).
Just in case you are not using MySQL 8+, you can still simulate ROW_NUMBER using a correlated count query:
(
SELECT 1 AS idx, name,
(SELECT COUNT(*) FROM users u2 WHERE u2.registration_date < u1.registration_date) rn
FROM users u1
)
UNION ALL
(
SELECT 2, name,
(SELECT COUNT(*) FROM food f2 WHERE f2.popularity < f1.popularity) rn
FROM food f1
)
ORDER BY
rn,
idx;
DECLARE #id INT;
SET #id :=0;
SELECT id,t.* FROM(
SELECT u.*,(#id:=#id+1) AS id
FROM users u
ORDER BY registration_date
)
UNION ALL
(
SELECT f.*,(#id:=#id+2) AS id
FROM food f
ORDER BY popularity
)t
ORDER BY id;

I want to merge two tables [Tabl1] and [Tabl2] and show result by ID where [Tabl1].[names] = 'any string name'?

In my example where name like '' show all value tabl2 with tabl1
SELECT *
FROM
(SELECT
ID, names, NULL AS address, work, note
FROM
Tabl1
UNION
SELECT
ID, name, address, NULL, NULL
FROM
Tabl2) as x
ORDER BY
id, note DESC, address
With CTE_NAME(ID, names) --Column names for Temporary table
AS
(
SELECT ID , NAME FROM TABLE1
UNION
SELECT ID , NAME FROM TABLE2
)
SELECT * FROM CTE_NAME --SELECT or USE CTE temporary Table
WHERE name = "x"
ORDER BY ID
You'll need to use UNION to combine the results of two queries. In your case:
SELECT ID, names, NULL AS address, work, note
FROM Tabl1
GROUP BY names
UNION ALL
SELECT ID, name, address, NULL, NULL
FROM Tabl2
GROUP BY Tabl3
Note - If you use UNION ALL as in above, it's no slower than running the two queries separately as it does no duplicate-checking.

return blank row when no records sql server

I have a table tblAccount. I want to get the top 4 rows from the table. If no record is there, I want to get 4 blank rows.
select *
from tblAccount
o/p
----
AccountID AccountNo
1 #101
2 #102
NULL NULL
NULL NULL
The above should be the result if two records are there.
SQLFiddle demo
select TOP 4 AccountID,AccountNo
from
(
select 0 as srt,AccountID,AccountNo from tblAccount
union all
select 1 as srt,NULL as AccountID, NULL as AccountNo
union all
select 2 as srt,NULL as AccountID, NULL as AccountNo
union all
select 3 as srt,NULL as AccountID, NULL as AccountNo
union all
select 4 as srt,NULL as AccountID, NULL as AccountNo
) as t
order by srt,AccountID
This should work. You could do the same thing with a temp table, just give it the right number of fields and rows.
with meganull(a,b) as (
select CAST(null as int),
CAST(null as varchar(max))
union all
select *
from meganull
)
select top 4 *
from (
select *
from tblAccount
union all
select *
from meganull) as sq
try this...
Select * FROM (VALUES (1),(2),(3),(4)) AS value(tmpID) left join
(
select Row_Number() over (order by AccountID) as RowCtr, *
from tblAccount
) accountTable on tmpID = accountTable.RowCtr
I used a similar technique when I wanted a way to force a certain number of rows to display in a SSRS report, where the blank rows represented empty spots in a physical rack.

Search in two tables and order by occurrence of term

I have this query to search in two SQL tables. I am looking for a way to sort the result by occurrence. This is my query:
SELECT `parent_id`
FROM wp_forum_posts
WHERE text LIKE '%{$term1}%'
UNION
SELECT `id`
FROM wp_forum_threads
WHERE subject LIKE '%{$term1}%
Which is the best way, to get the results ordered?
The trick is first to use UNION ALL, which preserves duplicates (ordinary UNION removes duplicates), then select from that result. This query should do it:
select * from (
select parent_id as mID, count(*) as cnt
from wp_forum_posts
where text like '%{$term1}%'
group by 1
UNION ALL
select id, count(*)
FROM wp_forum_threads
where subject like '%{$term1}%
group by 1) x
order by 2, 1
Assumes ID and parent_ID are not duplicates in tables otherwise you can get 2 rows per an id... and would you want them summed together if so then are parent_ID and ID related?
Select mID, cnt
FROM
(SELECT `parent_id` as mID, count(`parent_ID`) as cnt
FROM wp_forum_posts
WHERE text LIKE '%{$term1}%'
Group by `parent_ID`
UNION
SELECT `id`, count(`id`) as cnt
FROM wp_forum_threads
WHERE subject LIKE '%{$term1}%
GROUP BY `id`)
Order by cnt ASC, mID

Selecting the Max and Min records in one MySQL command

I need to be able to select two records from a table based on ID.
I need the first one, and the last one (so min, and max)
Example:
Customer
ID NAME
1 Bob
50 Bob
Any ideas? Thanks.
SELECT MIN(id), MAX(id) FROM tabla
EDIT: If you need to retrive the values of the row you can do this:
SELECT *
FROM TABLA AS a, (SELECT MIN(id) AS mini,
MAX(id) AS maxi
FROM TABLA) AS m
WHERE m.maxi = a.id
OR m.mini = a.id;
Is this what you are looking for?
select id, name from customers where id = ( select max(id) from customers )
union all
select id, name from customers where id = ( select min(id) from customers )
Now I have tested this type of query on a MySQL database I have access, and it works. My query:
SELECT nome, livello
FROM personaggi
WHERE livello = (
SELECT max( livello )
FROM personaggi )
If ties for first and/or last place are not a concern, then consider the following query:
(SELECT id, name FROM customers ORDER BY id DESC LIMIT 1)
UNION ALL
(SELECT id, name FROM customers ORDER BY id LIMIT 1);
It worked for me:
select * from customer where id in ((select min(id) from customer),(select max(id)
from customer));
SELECT MIN(value), MAX(value) FROM table