I have two table 'topic' and 'subcategory'
I am using this query--
Select * from `subcategory` as s
Inner join `topic` as f
WHERE s.`Subcategory_id` = f.`Subcategory_id
My result shows like
Category_id Subcategory_id Post_id time
2 2.3 4 2012-12-01
1 1.5 5 2013-01-20
1 1.3 6 2013-03-18
There's also other columns... but all I want is to select the latest Post_id and Subcategory_id of one Category_id ... that means here Category 1 has two Subcategory it will select only the latest(here 1.3) and same result all the time for all Category when database will grown larger. What will be the next query or how could I change the existing query to gain my desired result?
SELECT Post_Id, Subcategory_Id from subcategory as s, topic as t where
s.Subcategory_id = t.Subcategory_id and time = (
SELECT Max(time) from subcategory as s1, topic as t1 where
s1.Subcategory_id = t1.Subcategory_id and s1.Category_id = s.Category_id
);
Something like that, I think, will work.
SELECT TOP 1 ... ORDER BY whatever column determines "the latest"
e.g.
SELECT TOP 1 ... ORDER BY TIME DESCENDING
Or in case of mysql:
SELECT ... ORDER BY TIME DESCENDING LIMIT 1
Join your topic table with following query:
SELECT s.* FROM subcategory s
Inner JOIN (SELECT s1.Category_id,
MAX(s1.time1) AS max_time
FROM subcategory s1
GROUP BY s1.Category_id) y
ON y.Category_id = s.Category_id AND y.max_time = s.time1
Related
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 want to count all rows base on the category column, example below, category apple has two rows, so get the the category name apple. Thank you.
id user_id category
1 2 apple
2 4 banana
3 6 apple
4 7 berry
//Count all rows, apple has two row, the largest count, so then get the the category name apple
Use Group by to count the category.
Then order the result set in descending order of the count and select the top 1.
Query
select t.category
from
(
select category,
count(category) as cat_count
from fruits
group by category
)t
order by t.cat_count desc limit 1;
SQL Fiddle
If multiple category having same max count. Then,
Query
select t.category
from
(
select category,
count(category) as cat_count
from fruits
group by category
)t
where t.cat_count =
(
select count(category) as cat_count
from fruits
group by category
order by count(category) desc
limit 1
);
SQL Fiddle
Add limit if you need top 1,2,3 like that limit X x is 1,2,3
Select count(*) as total,category from table group by category from table order by count(*) desc
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)
I have a table which stores clients like this:
id name
-- ----
1 John
2 Jane
...
I also have another table which stores links created by clients:
id client_id link created
-- --------- ---- -----------
1 1 ... 2015-02-01
2 1 ... 2015-02-26
3 1 ... 2015-03-01
4 2 ... 2015-03-01
5 2 ... 2015-03-02
6 2 ... 2015-03-02
I need to find how many links a client has created today, this month and during all the time. I also need their name in the result, so I'll be able to craete a HTML table to display the statistics. I thought I can code as less as possible like this:
$today = $this->db->query("SELECT COUNT(*) as today, c.id as client_id, c.name FROM `links` l JOIN `clients` c ON l.client_id = c.id WHERE DATE(l.created) = CURDATE() GROUP BY c.id");
$this_month = $this->db->query("SELECT COUNT(*) as this_month, c.id as client_id, c.name FROM `links` l JOIN `clients` c ON l.client_id = c.id WHERE YEAR(l.created) = YEAR(NOW()) AND MONTH(l.created) = MONTH(NOW()) GROUP BY c.id");
$yet = $this->db->query("SELECT COUNT(*) as yet, c.id as client_id, c.name FROM `links` l JOIN `clients` c ON l.client_id = c.id WHERE GROUP BY c.id");
And then merge them in PHP as I asked HERE before, like this:
$result = array_replace_recursive($today, $this_month, $yet);
So I'll be able to loop into the result and print my HTML table.
But there are logical problems here. Everything works fine, but the result in a month is a wrong number, forexample the whole created links of one person is 1 but it shows 4 in the monthly counter! I also tried to use RIGHT JOIN in SQL query to get all clients, so array_replace_recursive in PHP could work fine as I think it doesn't work properly at the moment, but no success and got wrong results again.
Can anyone show me a way to make the job done?
This query should do it for today
$query_today="
SELECT name, id AS user_id, (
SELECT COUNT( * )
FROM links
WHERE client_id = user_id AND created = '2015-03-02'
) AS alllinks
FROM clients"
adjust the WHERE clause in the subquery for months and all
$query_month="
SELECT name, id AS user_id, (
SELECT COUNT( * )
FROM links
WHERE client_id = user_id AND created like '2015-03%'
) AS alllinks
FROM clients"
$query_all="
SELECT name, id AS user_id, (
SELECT COUNT( * )
FROM links
WHERE client_id = user_id
) AS alllinks
FROM clients"
I just wrote this query for my tables: NEWS and NEWS-CATEGORIES in order to count the items of each category:
SELECT DISTINCT CAT.cid, CAT.c_title, N.n_category, count(*) AS cat_count
FROM news N
inner join news - categories CAT
on CAT.cid = N.n_category
GROUP BY N.n_category
but the problem is that it just shows me the categories which contains news! but I wana get all of the categories even the ones with empty news...
my NEWS table is:
nid | n_category | etc
my NEWS-CATEGORY table is:
cid | c_title | etc
Thanks for your help
Regards
Try this:
SELECT
CAT.cid,
CAT.c_title,
count(N.n_category) AS cat_count
FROM `news-categories` CAT
LEFT JOIN `news` N
ON CAT.cid = N.n_category
GROUP BY CAT.cid,
CAT.c_title
Use LEFT JOIN:
SELECT CAT.cid, CAT.c_title, IFNULL(COUNT(N.n_category), 0) AS cat_count
FROM `news-categories` AS CAT
LEFT JOIN news AS N ON CAT.cid = N.n_category
GROUP BY CAT.cid
Things to note: 1) You have to use a column from news in the COUNT() expression, not COUNT(*), so that the null match is not counted. 2) There's no need to select N.n_category, since that's always equal to CAT.cid and you're already selecting that. 3) The GROUP BY column has to be from the news-categories table -- you can't group by a column in the table that may not have any matching rows, since that value will always be NULL.
I'm just going to point out that you can do this with a subquery as well:
SELECT CAT.cid, CAT.c_title,
(SELECT COUNT(*) FROM news N WHERE CAT.cid = N.n_category)
FROM `news - categories` CAT;
Under some circumstances, this can even have better performance.