Question

I am using datatables with a modified ssp.class.php to allow for joins and other custom features. In the example below I only want to return results from computers for the first x rows sorted by their id. Because of this, I list computers with my conditions first then LEFT JOIN users followed by logs (the information I am after).

It works great, BUT because of the left joins I have empty results. For instance, while my result set only contains logs from the correct computers... if logs has no rows for a particular user/computer combination I have a row with empty log data, but with user/computer data which serves me no purpose.

Is my only option to include a WHERE condition to prevent null values in the logs join... WHERE logs.user_id != '' or is there some other logic I can do in the select that I am missing?

SELECT (*see note)
FROM
    ( SELECT account_id, computer_id, computer_name
        FROM computers
        ORDER BY computer_id ASC LIMIT 0, ".$_SESSION['user']['licenses']."
    ) as c
    LEFT JOIN users
        on users.computer_id = c.computer_id
    LEFT JOIN logs
        on logs.user_id = users.user_id
Était-ce utile?

La solution

You can use just JOIN for the table logs.

Autres conseils

You put a LIMIT in the Derived Table accessing the computers table returning nrows. When an Inner Join or a final WHERE-condition filters some rows you will get less than nrows.

If this is not ok for you and you always want nrows, the only way is to move the LIMIT after doing Inner Joins:

SELECT (*see note)
FROM computers as c
    JOIN users
        on users.computer_id = c.computer_id
    JOIN logs
        on logs.user_id = users.user_id
ORDER BY computer_id ASC LIMIT 0, ".$_SESSION['user']['licenses']."

But this will probably be [much] slower...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top