Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Count lines in select

 
   Database Help (Home) -> mySQL RSS
Next:  Backup Compression in SQL Server 2008  
Author Message
Sam

External


Since: Nov 14, 2009
Posts: 1



(Msg. 1) Posted: Sat Nov 14, 2009 8:41 am
Post subject: Count lines in select
Archived from groups: comp>databases>mysql (more info?)

I have a table t1:

id int unique
added timestamp

and second table t2:

eid int
id int unique

t2.id are IDs from t1. t2.eid with the same value can be found in t2
more than for one entry but they will all have different IDs.



I want to:

find all EID that have IDs added after some DATE. Result should count
how many of these IDs are there for the same EID. Result should be of
the form (this is example):

EID | counts
-------+-----------
39 | 3
45 | 1

which means that EID 39 has 3 IDs in t1 that were added after DATE and
45 has only 1.

How to do that?

 >> Stay informed about: Count lines in select 
Back to top
Login to vote
toby

External


Since: Aug 21, 2008
Posts: 6



(Msg. 2) Posted: Sat Nov 14, 2009 11:38 am
Post subject: Re: Count lines in select [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Nov 14, 11:41 am, Sam wrote:
> I have a table t1:
>
> id        int unique
> added timestamp
>
> and second table t2:
>
> eid  int
> id    int unique
>
> t2.id are IDs from t1. t2.eid with the same value can be found in t2
> more than for one entry but they will all have different IDs.
>
> I want to:
>
> find all EID that have IDs added after some DATE. Result should count
> how many of these IDs are there for the same EID.

SELECT eid, COUNT(*)
FROM t2 LEFT JOIN t1 USING(id)
WHERE added > d
GROUP BY eid

> Result should be of
> the form (this is example):
>
> EID  |  counts
> -------+-----------
>  39   |    3
>  45   |    1
>
> which means that EID 39 has 3 IDs in t1 that were added after DATE and
> 45 has only 1.
>
> How to do that?

 >> Stay informed about: Count lines in select 
Back to top
Login to vote
Jerry Stuckle

External


Since: Aug 11, 2004
Posts: 3779



(Msg. 3) Posted: Sat Nov 14, 2009 2:50 pm
Post subject: Re: Count lines in select [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Sam wrote:
> I have a table t1:
>
> id int unique
> added timestamp
>
> and second table t2:
>
> eid int
> id int unique
>
> t2.id are IDs from t1. t2.eid with the same value can be found in t2
> more than for one entry but they will all have different IDs.
>
>
>
> I want to:
>
> find all EID that have IDs added after some DATE. Result should count
> how many of these IDs are there for the same EID. Result should be of
> the form (this is example):
>
> EID | counts
> -------+-----------
> 39 | 3
> 45 | 1
>
> which means that EID 39 has 3 IDs in t1 that were added after DATE and
> 45 has only 1.
>
> How to do that?

Unless I'm missing something, this should just be a simple JOIN with a
GROUP BY clause:

SELECT t1.id, count(t2)
FROM t1
JOIN t2 ON t1.id = t2.eid
WHERE t1.`timestamp` < (desired timestamp value here)
GROUP BY t1.id



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex.RemoveThis@attglobal.net
==================
 >> Stay informed about: Count lines in select 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Select more than just * - Is it possible to run a query like this: SELECT *, DATEDIFF(CURDATE(), `updated`) AS `daysSinceUpdate` FROM `Admin` Or should I let PHP calculate the days since update?

select with regexp - i have a column "path" with values eg. +---------------+ | path | +---------------+ |/path/path/path| +---------------+ |/path/path | +---------------+ |/path | +---------------+ i want to select the row where there are ...

Random select - Hi, When I do a select with a limit I get always the same records. Usually this is the desidered effect. But now I want to do a random select. So every time I do a select I should get a different set of results. What is the best way to achieve this..

Select Query Help - I'm struggling - with my limited knowledge of SQL - to write a query that does what I want it to, and I was hoping that someone here may be able to help out. I have a table with two columns, user1 and user2, which store user ID's. For example: ..

Restoring select databases/tables from an --all-databases .. - I use the --all-databases switch to backup my entire database. Sometimes there's a need to restore individual databases or tables form the backup file. What command should I use for this? Thanks, Raffi
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]