ctcp Posted November 21, 2011 Share Posted November 21, 2011 table1 users | id | login | ref | | 26757| alex| 0 | 26758 | php1| 26757 | 26759 | php2| 26757 table2 liked |id | user_id | | 1 | 26758 | | 2 | 26758 | | 3 | 26758 | | 4 | 26758 | | 5 | 26759 | i whant to count user_id from liked where login from users i don't know if this is right way to count but i trying to run this query can sombady help me what is wrong or give me the another way to count user_id <?php $query_liked = "SELECT COUNT(user_id) AS Num FROM liked AND (SELECT ref FROM users WHERE login='$x')"; ?> error message You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND (SELECT ref FROM users WHERE login='simos')' at line 1 Quote Link to comment Share on other sites More sharing options...
codeprada Posted November 21, 2011 Share Posted November 21, 2011 Your SQL query is wrong because your missing the WHERE clause and the AND ... isn't suppose to be there. SELECT COUNT(user_id) AS Num FROM liked WHERE user_id = $x Quote Link to comment Share on other sites More sharing options...
ctcp Posted November 21, 2011 Author Share Posted November 21, 2011 Your SQL query is wrong because your missing the WHERE clause and the AND ... isn't suppose to be there. SELECT COUNT(user_id) AS Num FROM liked WHERE user_id = $x im using 2 difrent tables mate Quote Link to comment Share on other sites More sharing options...
ctcp Posted November 21, 2011 Author Share Posted November 21, 2011 <?php // start count referrals likes //$query_liked = "SELECT COUNT(user_id) AS Num FROM liked AND (SELECT ref FROM users WHERE login='$x')"; $query_liked = "SELECT COUNT(user_id) AS Num FROM (SELECT user_id FROM `liked` UNION ALL SELECT ref FROM `users` WHERE login='$x') AS `t`"; $result = mysql_query($query_liked) or die(mysql_error()); $row_count = mysql_result($result,0,"Num"); echo $row_count; //end count ?> i found this but i get as result all rows on table how to get result WHERE table1 ref ID = table2 user_id Quote Link to comment Share on other sites More sharing options...
abdfahim Posted November 21, 2011 Share Posted November 21, 2011 I am not sure whether I understand what exactly you want or not, but you can try this Select user_id, login, count(user_id) from liked,users where liked.user_id=users.id GROUP BY user_id Quote Link to comment Share on other sites More sharing options...
fenway Posted November 23, 2011 Share Posted November 23, 2011 What do you have so far? Hard to follow. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.