0

I'm having a minor issue with a tiny mysql query. I'm trying to select a few columns and at the same time count what ever comes out of the table and add the count result as another column. so let's say I have three friends

  • friend 1
  • friend 2
  • friend 3

I want to see another column says allfriends=>3

So I came up with something that look like this

SELECT friends.friend, users.avatar, COUNT(*) AS allfriends FROM users,friends

now what's that doesn't work. so I tried to group them but then I got a '1' for each row. so it was just count that individual row. My question is: is there a way to count a all the results and add them all together to give me a sum of what ever i retrieved of the tables

4
  • i am so very confuse after reading the question due to the wording, I have no idea what you are trying to achieve.Can you organize your question or add some examples...? Commented May 9, 2014 at 7:35
  • I'd suggest using some pseudo-code if you can't word it perfectly, like [no_of_friend = SELECT count(friend) FROM friends] , [no_of_avatar = ....] , [total_count = no_of_friend + no_of_avatar] e.t.c. Commented May 9, 2014 at 7:40
  • I'm sorry, I should've worked more on it. however, I'm trying to select a few columns or rows from two tables. at the same time count as much rows or results come out of the query and put them with the result as another column Commented May 9, 2014 at 7:42
  • Not sure what your goal is. Is it to get a list of user avatars with a count of their friends? What is the relation between FRINEDS and USERS? If you don't specify join condition you will get a cross-join hence 3*4=12 rows returned. Commented Jun 10, 2016 at 8:43

2 Answers 2

1

I am not a mysql pro so this is exhasting but helped me learn a lot in the process as well. here's a sql EXAMPLE that I made with my own DB to demostrate the concept. i hope this is what you want.

the core concept is to generate a auto_increment column for each table/query results, and then left / right join depends on which table have more rows. to make a auto increamenting column within 1 SQL is fiddly, what i did is initiate the @cnt variable by

(select @cnt := 0 AS rowNumber) as t0

but i never used that table. I then do

select (@cnt := @cnt + 1) AS rowNumber, ....

in the following table for the incrmental column.

the method have a critical weakness which is you have to know which column/query will return the most row and predefine the type of JOIN in the SQL. I am not sure if this helps you in any way but I'd like to share it non the less.

mysql> select t1.rowNumber,t1.friend, t2.rowNumber,t2.avatar from (select @cnt := 0 AS rowNumber) as t0 RIGHT JOIN (select (@cnt := @cnt + 1) AS rowNumber,friend from friends) as t1 using (rowNumber) LEFT JOIN (select @cnt := 0 AS rowNumber) as t4 using (rowNumber) RIGHT JOIN (select (@cnt := @cnt + 1) AS rowNumber,avatar from user) as t2 using (rowNumber);
+-----------+-------------+-----------+------------+
| rowNumber |    friend   | rowNumber |   avatar   |
+-----------+-------------+-----------+------------+
|         1 |        0001 |         1 |       0001 |
|         2 |        0002 |         2 |       0002 |
|         3 |        0003 |         3 |       0003 |
|         4 |        0004 |         4 |       0004 |
|         5 |        0005 |         5 |       0005 |
|         6 |        0006 |         6 |       0006 |
|         7 |        0007 |         7 |       0007 |
|         8 |        0008 |         8 |       0008 |
|         9 |        0009 |         9 |       0009 |
|        10 |        0010 |        10 |       0010 |
|      NULL |        NULL |        11 |       0011 |
|      NULL |        NULL |        12 |       0012 |
|      NULL |        NULL |        13 |       0013 |
|      NULL |        NULL |        14 |       0014 |
|      NULL |        NULL |        15 |       0015 |
|      NULL |        NULL |        16 |       0016 |
|      NULL |        NULL |        17 |       0017 |
|      NULL |        NULL |        18 |       0018 |
|      NULL |        NULL |        19 |       0019 |
|      NULL |        NULL |        20 |       0020 |
+-----------+-------------+-----------+------------+
20 rows in set (0.00 sec)
Sign up to request clarification or add additional context in comments.

Comments

0

This should work i think because your question is confusing little bit

SELECT friends.friend, users.avatar,xyz.* FROM users,friends,(SELECT COUNT(*) AS allfriends FROM users,friends)xyz

1 Comment

i think that might not work, wouldn't it return 12 rows if for example there are 3 friend and 4 avatar ? (3 * 4 = 12)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.