Joining two tables without any relationship

  mysql, php, sql

If i understood it correctly I need related column to join two tables, which I dont. The third select statement got no releationship with the first two, Its just a count of a table.

(SELECT * 
FROM groups 
LEFT OUTER JOIN user_groups 
ON groups.id = user_groups.group_id 
WHERE user_groups.user_id = '".$_SESSION['id']."'
) 
UNION
( 
SELECT * 
FROM groups 
RIGHT OUTER JOIN user_groups 
ON groups.id = user_groups.group_id 
WHERE user_groups.user_id = '".$_SESSION['id']."'
)
(
   SELECT COUNT(*) as cnt 
   FROM user_groups 
   WHERE group_id =  '".$_SESSION['id']."'
)";

The first two select statements works but Im trying to add the third one into it and cant find what to do or search for.

I want to echo out the result like this in the end:

$result = $con->query($query);
if($result){
            while($row = $result->fetch_object()) 
            {   
echo '<div class = "flex"><h3 id = "'.$row->id.'">'.echo $row['cnt'];.'</h3></div>';
            }
}

What is this thing called or am I just missing something simple?

Source: Ask PHP

LEAVE A COMMENT