mysql select UNION with AND problem -
in database, table friendship has id, username , friendusername. when user accept other user friend, 1 row of record saved in table (not 2 rows). want random select 15 users , display pictures, mysql code :
<?php $query120 = "select frenusername friendship username='{$username2}' union select username friendship friendusername='{$username2}' , rand()<(select ((15/count(*))*10) friendship) order rand() limit 15"; $result120 = mysql_query($query120,$connection) or die (mysql_error()); confirm_query($result120); while($userinfo120 = mysql_fetch_array($result120)){ $frenusername= $userinfo120['frenusername']; $username = $userinfo120['username']; //this hold empty value, why? $query121 = "select picturemedium users username='{$frenusername} or username='{$username}' limit 1"; //display picture } } ?>
my problem 1 : why $username
hold empty value?
my problem 2 : statement and rand()<(select ((15/count(*))*10) friendship) order rand() limit 15"
random select 15 records table. supposedly need write on both union statements right? (meaning need write 2 times). if so, show 30 records instead of 15 records. should change become 8/count limit 8
each? or there other way avoid duplicate , statement?
if need random set of 15 friends of username2
, don't need union
:
(select username friendship (friendusername='{$username2}' or username='{$username2}') , rand()<(select ((15/count(*))*10) friendship) order rand() limit 15)
the union select either select
. in sql, first select
of usernames in friendship table, second select
random 15, union
select either of 2 select
s record if in either of 2 select
results. that's why you're seeing users.
Comments
Post a Comment