Solved Mysql Conditional Query Assistance

February 6, 2014 at 23:04:50
Specs: Windows 7
This is what I have so far.


$result = $DB->query("(SELECT f1.member_id as friend_id, f1.request_date, m.*
FROM " . DB_PREFIX . "friends AS f1, " . DB_PREFIX . "members AS m
WHERE f1.friend_id='$member_id' AND f1.member_id=m.member_id AND f1.pending='0' $wheresql)
UNION
(SELECT f2.friend_id, f2.request_date, m.*
FROM " . DB_PREFIX . "friends AS f2, " . DB_PREFIX . "members AS m
WHERE f2.member_id='$member_id' AND f2.friend_id=m.member_id AND f2.pending='0' $wheresql)
ORDER BY username ASC
LIMIT $limit");

Table = prefix_friends

  
   
 member_id  |  friend_id  | request_date  | pending
  3         |    18       |  1299658055   |  0
  6         |    22       |  1366802565   |  0
  7         |    3        |  1366802566   |  0


 Table = prefix_members

  member_id  | group_id | username
   1         |  1       | Tom
   2         |  2       | Betty
   3         |  2       | Gena
   4         |  5       | Richard
   5         |  8       | Angie
   6         |  5       | Keith
   7         |  8       | Lori
 


What I'm trying to do is retrieve only the records that are in group_id 2 and group_id 5 . Unable to grasp exactly how to structure the query. Any help would be greatly appreciated.

message edited by Lori_S


See More: Mysql Conditional Query Assistance

Report •

#1
February 7, 2014 at 07:42:03
✔ Best Answer
Your sql statement has a number of problems so I'm not exactly sure what you want returned, but try this sql.

SELECT   m.member_id, m.group_id, m.username, f.friend_id, f.request_date
FROM     prefix_members as m
JOIN     prefix_friends as f on m.member_id = f.member_id
WHERE    m.group_id IN(2,5)
  AND    m.member_id = $member_id
  AND    f.pending = 0
ORDER BY m.username ACS;


Report •
Related Solutions


Ask Question