Get records as per the given ids' mysql - mysql

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

Related

MySQL want to sum of two columns with UNION

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'

How can I distinctively count values I recieve from a union query?

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

MySQL Query Optimization for running total query - How can I reduce query exectuion time?

Problem
I have a query that I pasted below. The problem I face is how can I trim the latency to under the current time of about 10 seconds.
set# csum = 0;
SELECT Date_format(assigneddate, '%b %d %Y') AS assigneddate, (#csum: = #csum + numactionitems) AS totalactionitems
FROM(
SELECT assigneddate,
Sum(numactionitems) AS numactionitems FROM(
SELECT assigneddate,
Count( * ) AS numactionitems FROM(
SELECT *
FROM(
SELECT actionitemtitle,
actionitemstatement,
altownerid,
approvalstatement,
assigneddate,
assignorid,
closeddate,
closurecriteria,
closurestatement,
criticality,
duedate,
ecd,
notes,
ownerid,
Concat(lastname, ', ', firstname) AS owner,
cnames2.categoryvalue AS `team`,
cnames2.categorynameid AS `teamid`,
cnames3.categoryvalue AS `department`,
cnames3.categorynameid AS `departmentid`,
cnames4.categoryvalue AS `source`,
cnames4.categorynameid AS `sourceid`,
cnames5.categoryvalue AS `project_phase`,
cnames5.categorynameid AS `project_phaseid`,
ac1.actionitemid FROM actionitemcategories AS ac1 INNER JOIN actionitems AS a INNER JOIN users AS u INNER JOIN(
SELECT actionitemid AS a2id,
categorynameid AS c2 FROM actionitemcategories WHERE categoryid = 195) AS ac2 INNER JOIN categorynames AS cnames2 ON cnames2.categorynameid = ac2.c2 AND ac1.categoryid = 195 AND a.actionitemid = ac2.a2id AND ac1.actionitemid = a.actionitemid AND a.ownerid = u.userid INNER JOIN(
SELECT actionitemid AS a3id,
categorynameid AS c3 FROM actionitemcategories WHERE categoryid = 200) AS ac3 INNER JOIN categorynames AS cnames3 ON cnames3.categorynameid = ac3.c3 AND ac2.a2id = ac3.a3id INNER JOIN(
SELECT actionitemid AS a4id,
categorynameid AS c4 FROM actionitemcategories WHERE categoryid = 202) AS ac4 INNER JOIN categorynames AS cnames4 ON cnames4.categorynameid = ac4.c4 AND ac3.a3id = ac4.a4id INNER JOIN(
SELECT actionitemid AS a5id,
categorynameid AS c5 FROM actionitemcategories WHERE categoryid = 203) AS ac5 INNER JOIN categorynames AS cnames5 ON cnames5.categorynameid = ac5.c5 AND ac4.a4id = ac5.a5id) s WHERE 1 = 1) f GROUP BY assigneddate UNION ALL(
SELECT a.date AS assigneddate,
0 AS numactionitems FROM(
SELECT '2015-03-05' + INTERVAL(a.a + (10 * b.a) + (100 * c.a)) day AS date FROM(
SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a CROSS JOIN(
SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b CROSS JOIN(
SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c) a) ORDER BY assigneddate ASC) t GROUP BY assigneddate LIMIT 282) t
WHERE assigneddate != '0000-00-00'
Purpose of Query
The purpose of this query is to get all records that contain date values and to collect the running count of all records that fall on a certain date. Date values are computed within the sqlfiddle below. It's final purpose is to be displayed in a graph that takes the running total as a line graph. It will be counting upwards so it is a growing graph.
The graph I am displaying it in is called a build-up graph of all my records (action items with date values).
Description of Issue
My problem is that I am getting the results of the query in at least 10 seconds.
Question
How can I accelerate and reduce the latency of the query so that I will not stall the loading of my graph?
Complete Schema and portion of my above query that Runs Successfully
(I am having difficulty getting the main query to run at all on sqlfiddle, though I can run it from my own machine).
http://sqlfiddle.com/#!9/865ee/11
Any help or suggestions would be tremendously appreciated!
EDIT
ADDED Sample Screenshot of my Categories Interface
Category (First Table) has a field called categoryname which assumes one of 4 values can be expanded or deleted which is - Team, Department, Source, Project_Phase.
CategoryName (Second Table) has a field called categoryvalue which is the actual allowed value for each category (First Table)
Example - Team 1, Team 2, Team 3 are categoryvalues within categoryname and corresponding the category of Team.
Category
Start by making that table of dates a permanent table, not a subquery.
This construct performs very poorly, and can usually be turned into JOINs without subqueries:
JOIN ( SELECT ... )
JOIN ( SELECT ... )
This is because there is no index on the subqueries, so full scans are needed.
Provide EXPLAIN for the entire query.
Addenda
A PRIMARY KEY is a key; don't add another key with the same column(s).
EAV schema leads to complexity and sluggishness that you are encountering.
Don't use TINYTEXT; it slows down tmp tables in complex queries; use VARCHAR(255). Don't use VARCHAR(255), use VARCHAR with a realistic limit.
Why do you need both categories and categorynames?

MySQL select from custom set and compare with table data

Hi I'm trying to solve which elements doesn't exists in my database. In order to do so I want to compare list of integers (output from external script) with data in table. How to do such thing like:
SELECT * FROM (1,1,2,3,5,8,13...) l WHERE l NOT IN (select id from table1);
This is probably best done with a left outer join. But, your problem is creating the table of constants:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids
where ids.id NOT IN (select id from table1);
This can have odd behavior, if table1.id is ever NULL. The following works more generally:
SELECT *
FROM (select 1 as id union all select 2 union all select 3 union all select 5 union all
select 8 union all select 13 union all select 21 . . .
) ids left outer join
table1 t1
on ids.id = t1.id
where t1.id is null;
EDIT:
The size of a MySQL query is dictated by the parameter max_packet_size (see here). The most recent version has a limit of 1 Gbyte. You should be able to fit 18,000 rows of:
select <n> union all
into that limit, quite easily. Gosh, I don't even think it would be 1 megabyte. I would say, though, that passing a list of 18,000 ids through the application seems inefficient. It would be nice if one database could just pull the data from the other database, without going through the application.
If your set to compare is huge I'd recommend you to create a temporary table myids with the only column id, put there all your 18K values and run query like that:
select id from myids where myids.id not in (select id from table1);

Count same values in mysql query

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;