Q:

For each distinct ID in a table, count the number of related UIDs in a different table. ( SQL)

I have two tables, where one has unique ids and the other does not, sort of like this:
**Table1**
ID     OTHERSTUFF
id1    ...
id1    ...
id2    ...
id2    ...
id3    ...

**Table2**
UID    MORESTUFF
uid1   ...
uid2   ...
uid3   ...

Each ID appears as a substring of a UID, but it's possible that they appear in many, and I want to count how many. 
I've tried the following query (which doesn't give what I want):
select distinct ID, count(Table2.UID) 
from Table1
inner join Table2 on locate(ID, Table2.UID) > 0
group by ID

What I want this to be doing is the following: for each distinct ID in Table1, count the number of entries in Table2 whose UID contains the ID in question.  
What I think ends up happening is that for each ID is it ends up counting (# of corresponding UIDs)x(# of occurrences), and then returns the list of distinct (ID, count) pairs.  Indeed the result I get has multiple IDs whose count is quite large, but when I query UIDs which contain those specific IDs then I only get 1 each. 
How should I write my query differently so that each distinct ID only gets processed once? 

A:

You'll need to get the distinct id values in a subquery first:
select t1.ID, count(Table2.UID) 
from (SELECT DISTINCT ID FROM Table1) AS t1
inner join Table2 on locate(t1.ID, Table2.UID) > 0
group by t1.ID;

Alternatively, you could have ignored the duplicate joins (but, perhaps inadvertently, duplicate matches from table2 as well) by using COUNT(DISTINCT Table2.UID) in your original query.