mysql - How do I add two count(*) results together on two different tables? -
I have two tables: toys and games.
+ ------ -------------- + ------------------ + | Field | Type | + -------------------- + ------------------ + | Toy_id | Int (10) unsigned. | Small_keydie_id | Int (10) unsigned. + -------------------- + ------------------ + + -------- ------------ + ------------------ + | Field | Type | + -------------------- + ------------------ + | Game_ID | Int (10) unsigned. | Little_kid1 | Int (10) unsigned. | Small_kid2 Int (10) unsigned. | Little_kid3 | Int (10) unsigned. + -------------------- + ------------------ + < P> A small child can have several toys A small child can participate in several games at once. I want a question that will give me the total number of toys + which is associated with small children.
Actually, I want the sum of these two questions:
SELECT COUNT (*) from toys where little_kid_id = 900; Select game (*) from where little_kid1 = 900 or little_kid2 = 900 or little_kid3 = 900;
Is it possible to get it into a single SQL query? Obviously, I can programmatically add them, but this is less desirable.
(I know that the indexed example makes scheme schematic redundant. Suppose we can not change the schema.)
Wrap them and use the subkey:
select (select COUNT (*) from toys where small_keydie_id = 900) + (SELECT COUNT (*) From games where little_kid1 = 900 or small_kid2 = 900 or small_kid3 = 900) SAMCount Voila!
Comments
Post a Comment