I have this query:
SELECT Pedido1 from mydb.atendimentos
UNION ALL
SELECT Pedido2 from mydb.atendimentos
order by Pedido1 ASC
That gets me this result:
What I get when executing the query
Now what I it to deliver is:
Teste -> 3
Teste2 -> 1
Is there any way of doing this with a union?
This is called derived table.
http://www.programmerinterview.com/index.php/database-sql/derived-table-vs-subquery/
SELECT Pedido1, COUNT(*) AS PedidoCount
FROM
(
SELECT Pedido1 FROM mydb.atendimentos
UNION ALL
SELECT Pedido2 FROM mydb.atendimentos
) T
GROUP BY Pedido1
ORDER BY Pedido1
Related
This is the query i am executing
SELECT email,firstname,lastname FROM `sco_customer`
WHERE id_customer IN (7693,7693,7693,7693,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,7693,3,3,3,3,3,7693,7693,3,3,3,7693,3,3,3)
This gives me only two records as their are same number of id_customer is filtered i.e 7693,3
email firstname lastname
abc#any.com Test Mage
abc2#any.com User Mage
It should give the same number of records as much is the id_customer
Any thoughts how this can be achieved ?
Try below. Instead of WHERE clause you can generate a dummy table and join it with your main table.(WITH works for version 8 or above)
WITH SAMPLE AS
(
SELECT 7693 AS ID FROM DUAL
UNION ALL
SELECT 3 AS ID FROM DUAL
)
SELECT email,firstname,lastname FROM `sco_customer`
INNER JOIN SAMPLE ON SAMPLE.ID=ID_CUSTOMER
Below mysql version 8:
SELECT email,firstname,lastname FROM `sco_customer`
INNER JOIN (
SELECT 7693 AS ID FROM DUAL
UNION ALL
SELECT 3 AS ID FROM DUAL
)SAMPLE ON SAMPLE.ID=ID_CUSTOMER
The following statement should solve you problem:
SELECT email,firstname,lastname FROM `sco_customer`
join (select 7693 as id_customer union all
select 7693 union all
select 7693 union all
select 3 union all
select 3 union all
select 3
) tmp on sco_customer.id_customer = tmp.id_customer
I have the following two tables:
requests: id, file_id
request_filter_integers: id, request_filter_id, value
and I want to fetch data with the following query:
select `requests`.`id`, `sum_followers`.`value` as `sum_followers`
from `requests`
left join `request_filter_integers` as `sum_followers` on `sum_followers`.`request_id` = `requests`.`id` and `sum_followers`.`request_filter_id` in (63002331)
where `requests`.`file_id` in (3571851, 3574928, 3576373, 10000857, 10001207, 10001258, 10001516, 10001528)
order by `sum_followers`.`value` desc limit 51 offset 0
This query takes forever and I am not sure if there is an index/order issue or if it's generally limited by the way the table structure/join is setup.
requests contains about 150 million rows with 10k different file_ids. request_filter_integers has 130 millionen rows.
The where file_id IN clause in the query limits the requests rows to 1 million.
EXPLAIN for the above query returns:
and I have the following indexes:
requests table:
request_filter_integers table:
You have a request file_id column without index but used in where clause
so you should use a composite index for table request on columns (file_id, id )
and you should also avoid a IN clause for several values for this you could try using a join. (with a subquery on union or a a proper table for the IN values )
a subquery as
select 3571851 file_id
union
select 3574928
union
select 3576373
union
select 10000857
union
select 10001207
union
select 10001258
union
select 10001516
union
select 10001528
should return the same values and the your query could be modified this way
select `requests`.`id`, `sum_followers`.`value` as `sum_followers`
from `requests`
INNER JOIN (
select 3571851 file_id
union
select 3574928
union
select 3576373
union
select 10000857
union
select 10001207
union
select 10001258
union
select 10001516
union
select 10001528
) t o t.file_id = requests.file_id
left join `request_filter_integers` as `sum_followers` on `sum_followers`.`request_id` = `requests`.`id`
and `sum_followers`.`request_filter_id` in (63002331)
order by `sum_followers`.`value` desc limit 51 offset 0
I have two MySQL tables. Each table has the following fields:
p_id
hours_value
minute_value
I want to sum of the hours and minutes field of these two tables for a p_id or project_id. Below query did not provide me the expected result.
SELECT SUM(hours_value), SUM(minute_value)
FROM timesheet_master
UNION
SELECT `hours_value`
FROM timesheet_master_archive
WHERE `p_id` = '1'
I suppose you want to union the rows and then calculate the sums? That would be:
select sum(hours_value), sum(minute_value)
from
(
select hours_value, minute_value from t1 where p_id = 1
union all
select hours_value, minute_value from t2 where p_id = 1
) both_tables;
You can try below - for union, your no of columns should be equal in both select query
SELECT SUM(hours_value) as hrval, SUM(minute_value) as minval
FROM timesheet_master
UNION
SELECT `hours_value`,minute_value
ROM timesheet_master_archive WHERE `p_id` = '1'
The query I found:
SELECT SUM(hours_value) as hrval, SUM(minute_value) as minval
FROM timesheet_master WHERE `p` = '1'
UNION
SELECT `hours_value`,minute_value
FROM timesheet_master_archive WHERE `p_id` = '1'
So i have a query like
SELECT * FROM `catalog` WHERE `id` IN ('2','2','3','3','3');
And this return only 2 rows with id 2 and 3. It is possible make it return 5 rows (2 with id "2" and 3 with id "3") or add count as new column?
Not sure why you would want to do something like this, but instead of using an 'in' clause you could use an inner query:
select *
from `catalog` c,
(
select 2 ids
union all
select 2
union all
select 3
union all
select 3
union all
select 3
) k
where c.id = k.ids
Try something like this:
SELECT t.p,count(*) FROM
catalog,
(SELECT 2 as id
Union all select 2 as id
Union all select 3 as id
Union all select 3 as id
Union all select 3 as id)as t
where catalog.id = t.id
It can be done using temporary tables:
create temporary table arrayt (id int);
insert into arrayt values ('2'),('2'),('3'),('3'),('3');
select catalog.* from arrayt a LEFT JOIN catalog on (a.id=catalog.id);
if you need count
select count(catalog.id) as count,catalog.id as id from arrayt a LEFT JOIN catalog on (a.id=catalog.id) group by catalog.id;
I have a table with a list of names spread across five different columns. I'm trying to get the 6 most frequent distinct names. Each name will only appear in each record once. The five columns are name_1, name_2...name_5. And just for names sake call the table 'mytable'.
Any help would be much appreciated.
Here's one approach:
SELECT name, COUNT(1)
FROM ( SELECT name_1 AS name FROM mytable
UNION ALL SELECT name_2 AS name FROM mytable
UNION ALL SELECT name_3 AS name FROM mytable
UNION ALL SELECT name_4 AS name FROM mytable
UNION ALL SELECT name_5 AS name FROM mytable
) AS myunion
GROUP BY name
ORDER BY COUNT(1) DESC LIMIT 6
;
How many rows are there in the table?
try this:
SELECT iTable.iName, Count(iTable.iName) as TotalCount
FROM
(
SELECT DISTINCT name_1 as iName FROM myTable
UNION
SELECT DISTINCT name_2 as iName FROM myTable
UNION
SELECT DISTINCT name_3 as iName FROM myTable
UNION
SELECT DISTINCT name_4 as iName FROM myTable
UNION
SELECT DISTINCT name_5 as iName FROM myTable
) as iTable
GROUP BY iTable.iName
ORDER BY TotalCount DESC
LIMIT 6
You should be able to select all the names from each table and union the results together. Then you can count the number of times each name occurs.
select *
from
(
select name, count(*)
from (
select name from table1
union all
select name from table2
union all
select name from table3
union all
select name from table4
union all
select name from table5
)
group by name
order by count(*) desc
)
where rownum <= 6
UNION + subselect should work for you in this case.
SELECT name_1, COUNT(*) FROM (
SELECT name_1 FROM mytable
UNION ALL SELECT name_2 FROM mytable
UNION ALL SELECT name_3 FROM mytable
UNION ALL SELECT name_4 FROM mytable
UNION ALL SELECT name_5 FROM mytable
) AS names GROUP BY name_1 ORDER BY 2 DESC LIMIT 6;