Count same values in mysql query - mysql

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;

Related

Get records as per the given ids' 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

How to fetch Rows have not this column value in another table column value

I have a table like this:
Tb_Product_Options:
Product_Id Option_Id
1 5
1 7
2 3
3 9
3 6
Now I want to get all Product_Ids have not this values : '5,9'.
at this example my result must be: 2 (product_id)
You can aggregation :
select Product_Id
from table t
group by Product_Id
having sum ( Option_Id in (5,9) ) = 0;
If you want all columns then you can use NOT EXISTS :
select t.*
from table t
where not exists (select 1
from table t1
where t1.Product_Id = t.Product_Id and t1.Option_Id in (5,9)
);
In the old days, we would use an exclusion join:
SELECT DISTINCT x.*
FROM my_table x
LEFT
JOIN my_table y
ON y.stuff = x.stuff
AND y.other_stuff IN(some,values)
WHERE y.id IS NULL
This surely works:
select product_id
from Tb_Product_Options
where product_id not in (
select product_id
from Tb_Product_Options
where option_id in (5,9)
)

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'

repeat result multiple times in mysql

I have a table having id and no field, what I really want is the result raw will be repeated no filed times, if the no field is 2 then that raw must be repeated twice in result.
this is my sample table structure:
id no
1 3
2 2
3 1
now I need to get a result like:
1 3
1 3
1 3
2 2
2 2
3 1
I tried to write mysql query to get the result like above, but failed.
You need a table of numbers to accomplish this. For just three values, this is easy:
select t.id, t.no
from t join
(select 1 as n union all select 2 union all select 3
) n
on t.no <= n.no;
This query must do what you want to achieve:
select t.id, t.no from test t cross join test y where t.id>=y.id
not completely solve your problem, but this one can help
set #i=0;
select
test_table.*
from
test_table
join
(select
#i:=#i+1 as i
from
any_table_with_number_of_rows_greater_than_max_no_of_test_table
where
#i < (select max(no) from test_table)) tmp on no >= i
order by
id desc
EDIT :
This is on SQL Server. I checked online and see that CTEs work on MySQL too. Just couldn't get them to work on SQLFiddle
Try this, remove unwanted columns
create table #temp (id int, no int)
insert into #temp values (1, 2),(2, 3),(3, 5)
select * from #temp
;with cte as
(
select id, no, no-1 nom from #temp
union all
select c.id, c.no, c.nom-1 from cte c inner join #temp t on t.id = c.id and c.nom < t.no and c.nom > 0
)
select * from cte order by 1
drop table #temp

How to address multiple columns as one in MySQL?

Columns a, b and c contain some values of the same nature. I need to select all the unique values. If I had just one column I'd use something like
SELECT DISTINCT a FROM mytable ORDER BY a;
but I need to treat a, b and c columns as one and gett all the unique values ever occurring among them.
As an example, let this be a CSV representation of mytable, the first row naming the columns:
a, b, c
1, 2, 3
1, 3, 4
5, 7, 1
The result of the query is to be:
1
2
3
4
5
7
UPDATE: I don't understand why do all of you suggest wrapping it in an extra SELECT? It seems to me that the answer is
(SELECT `a` AS `result` FROM `mytable`)
UNION (SELECT `b` FROM `mytable`)
UNION (SELECT `c` FROM `mytable`)
ORDER BY `result`;
isn't it?
So you want one column all with unique values from a, b and c? Try this:
(select a as yourField from d1)
union
(select b from d2)
union
(select c from d3)
order by yourField desc
limit 5
Working example
Edited after requirements changed... There you have the order by and limit you requested. Of course, you'll get only 5 records in this example
sorry i miss understood your question. here is updated query.
select a from my table
UNION
select b from my table
UNION
select c from my table
SELECT tmp.a
FROM
(SELECT column_1 AS a
FROM table
UNION
SELECT column_2 AS a
FROM table
UNION
SELECT column_3 AS a
FROM table) AS tmp
ORDER BY `tmp`.`a` ASC
try this:
SELECT b.iResult
FROM
(SELECT a as iResult FROM tableName
UNION
SELECT b as iResult FROM tableName
UNION
SELECT c as iResult FROM tableName) b
ORDER BY b.iResult
LIMIT BY 10 -- or put any number you want to limit.