This is my table, I should fetch the MAX (id) of each status_id.
id status_id
10 1
11 1
12 2
13 2
14 2
15 4
16 4
So, I use this sql query, it works true and fetchs me all max ID.
select status_id, max(id) as max FROM `table`
where status_id in (1,2,3,4) group by status_id
This sql command fetchs me 3 MAX id using while.
11, 14, 16....
You see, there is not any suitable id to 3rd status_id. And if there is not any suitable id to 3rd status_id just mark it as zero. So I want that sql will bring these results:
11, 14, 0, 16
You can create a subquery which basically has all the ID's you need and have a left join against it.
SELECT a.status_ID,
IFNULL(MAX(b.id), 0) maxVal
FROM
(
SELECT 1 status_ID UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4
) a
LEFT JOIN `table` b ON a.status_id = b.status_id
GROUP BY a.status_id
SQLFiddle Demo
You can join with a temporary dummy table containing all ids and a stats_id value of 0:
SELECT dummy.status_id, COALESCE(MAX(id), 0) AS max
FROM (
SELECT 1 status_id
UNION SELECT 2 status_id
UNION SELECT 3 status_id
UNION SELECT 4 status_id
) dummy
LEFT JOIN `table`
ON dummy.status_id = table.status_id
GROUP BY dummy.status_id
But this does not scale and is a maintenance nightmare (you have to change the dummy-select in case a new status_id turns up). If you have a table containing ALL status_ids, replace the dummy-select with that table.
Related
Using the below select statement:
select
spec_sheet_color_comb.id,
spec_sheet_color_comb.pairs*2 as total
from
spec_sheet_color_comb
where
spec_sheet_color_comb.id_spec_sheet IN (4814)
And getting this result:
id total
79928 5
Now, I want to split this result according to the TOTAL quantity and get this result, 5 rows showing the ID:
Result
79928
79928
79928
79928
79928
It is really important, Thanks
Suppose you have a table having 2 columns id and total. (You can use your query instead of the table).
You need a collateral table (or view) with all the numbers 1..max_number_you_need
e.g.
(select 1 as number
union
select 2 as number
...
select 999 as number)
Then you can use the number source table joining your table you need to multiply rows
select id
from the_table t
join (table with numbers) n on n.number<=t.total
UPDATE: an example
SELECT id
from (select 1 as id, 5 as total
union
select 2 as id, 3 as total) t
join (select 1 as number
union
select 2 number
union
select 3 number
union
select 4 number
union
select 5 number) num on num.number<=t.total
order by id
I want to select rows up to a certain number (Size).
My SQL (SQL Fiddle):
id user_id storage
1 1 1983349
2 1 42552
3 1 367225
4 1 1357899
37 1 9314493
I want to select only all rows up to a certain number (size).
Like this here:
Select * from uploads where storage < 410000
it should get something like this here:
id user_id storage
2 1 42552
3 1 367225
The Summary of ID '2' and '3' is 409777.
You need some way of getting a cumulative sum. In MySQL, the easiest way uses variables:
select u.*
from (select u.*, (#s := #s + storage) as cume_storage
from uploads u cross join (select #s := 0) params
order by id
) u
where cume_storage < 410000;
id parent_id child_id
1 1 1
2 2 2
3 2 2
4 1 1
I have a table from which i need to get the common values from data when i query it with id... for eg if id=2 and id=3 then return
id parent_id
2 2
3 2
i have tried this after hunting a lot through various examples :
SELECT ta.user_id,ta.interest_parent_id,ta.interest_child_id
FROM user_interest ta
WHERE ta.user_id=2 AND
(SELECT COUNT(*) FROM user_interest tb
WHERE ta.interest_parent_id=tb.interest_parent_id
AND tb.user_id=3 )>1
but it responds with only:
id parent_id
2 2
any help :( im using a mysql database with php/codeigniter to do the scripting
You can give it a try:
SELECT
tOne.id,
tOne.parent_id
FROM
(
SELECT
*
FROM user_interest A
WHERE A.id IN (2,3)
) tOne
INNER JOIN
(
SELECT
*
FROM user_interest A
WHERE A.id IN (2,3)
) tTwo
ON tOne.parent_id = tTwo.parent_id
AND tOne.id <> tTwo.id
ORDER BY tOne.parent_id;
SQL FIDDLE DEMO
Any suggestion towards optimization of the query is welcome.
EDIT: SQL FIDDLE
You can make a sub SELECT:
SELECT * FROM table WHERE Name IN (SELECT Name FROM table GROUP BY Name HAVING count(*) > 1)
I have two MySQL table with contain values like
table1
id email_id
===========
1 abc#xyz.com
2 bbc#xy.com
3 gty#xyz.com
4 iut#xyz.com
5 tyk#xy.com
table2
id name email_id
===========
1 abc abc#xy.com
2 bbc bbc#xy.com
3 gty gty#xy.com
4 iut iut#xy.com
5 tyk tyk#xy.com
6 tyr tyr#xy.com
7 iut iut#xy.com
Result
table2
id name email_id
===========
1 abc abc#xyz.com
2 bbc bbc#xy.com
3 gty gty#xyz.com
4 iut iut#xyz.com
5 tyk tyk#xy.com
6 tyr tyr#xy.com
7 iut iut#xyz.com
You can see in my first table is a combination of #xy.com and #xyz.com. So i need to change all #xy.com to #xyz.com in table2 whether if table1 is same #xyz.com.
Example: case1- in table1 abc#xyz.com is available and its #xyz format, so in table2 i needs to change it as abc#xyz.com
case2- in table1 bbc#xy.com is available and its in #xy format, So in table2 i need not change bbc#xyz.com. i can leave it as it is.
i think you understood my issue and please give me a mysql query for solve it.Thanks in advance
You can join together table1 and table2 and then compare the length of the email addresses for each id. Since you want to conditionally replace the #xy format with the #xyz format, you can simply choose the longer email address to retain in the query.
SELECT t2.id, t2.name,
CASE WHEN CHAR_LENGTH(t2.email_id) > CHAR_LENGTH(COALESCE(t1.email_id, ''))
THEN t2.email_id ELSE t1.email_id END AS email_id
FROM table2 t2 LEFT JOIN table1 t1 ON t2.id = t1.id
i had written in sql server
create table #stack_table1(id int identity(1,1),
email_id varchar(1000))
create table #stack_table2(id int ,
name varchar(50),
email_id varchar(1000)
)
insert into #stack_table1(email_id)
(select ('abc#xyz.com')
union
select ('bbc#xy.com')
union
select ('gty#xyz.com')
union
select ('iut#xyz.com')
union
select ('tyk#xy.com')
)
insert into #stack_table2(id,name,email_id)
(
select 1,'abc','abc#xy.com'
union
select 2,'bbc','bbc#xy.com'
union
select 3,'gty','gty#xy.com'
union
select 4,'iut','iut#xy.com'
union
select 5,'tyk','tyk#xy.com'
union
select 6,'tyr','tyr#xy.com'
union
select 7,'iut','iut#xy.com'
)
select * from #stack_table1
select * from #stack_table2
tables were created
update statement will be as follows
UPDATE st2
SET st2.email_id = st1.email_id
FROM #stack_table1 st1
JOIN #stack_table2 st2 ON st2.NAME = substring(st1.email_id, 1, CHARINDEX('#', st1.email_id, 1) - 1)
if only select query needed please use this
SELECT st2.id
,st2.NAME
,coalesce(st1.email_id, st2.email_id)
FROM #stack_table1 st1
RIGHT JOIN #stack_table2 st2 ON st2.NAME = substring(st1.email_id, 1, CHARINDEX('#', st1.email_id, 1) - 1)
Hello I have the following table design
ID account_id score date
------------------------------------------
1 500 4 x
2 764 4 x
3 500 6 x
4 500 7 x
5 764 5 x
I'm trying to get all rows with the latest account_id entry
so my code should return
ID account_id score date
------------------------------------------
4 500 7 x
5 764 5 x
I tried the following code but it seems to return the first entry but with the latest date
SELECT account_id,score, max(date) from table group by account_id
Case 1: If id is auto-increment column or max(id) means latest row.
select * from
(select * from table_name
order by id desc) temp
group by account_id
Case 2: If date column decides latest row then replace id by date in order clause and group clause.
try it-
SELECT distinct a.id,a.account_id,b.score,b.date
FROM mytable b
JOIN
(
SELECT account_id,MAX(id) AS id
FROM mytable
GROUP BY account_id
) a ON a.account_id=b.account_id
ORDER BY a.id;
This question is just a duplicate of SQL Select only rows with Max Value on a Column
There you'll find a good explanation.
SELECT a.*
FROM table a
INNER JOIN (
SELECT `account_id`,MAX(`date`) AS latest FROM table GROUP BY account_id
) b
ON a.`date` = b.`latest` AND a.`account_id` = b.`account_id`
ORDER BY `date` DESC
Reference :
Using ORDER BY and GROUP BY together
Try using this query it works fine
Select a.account_id,a.score,a.date
from authors as a
join
(Select account_id,max(date) as date
from authors
group by account_id) as d
on(d.date=a.date)