select from another table if rows do or do not exist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dannix
    New Member
    • Jul 2008
    • 1

    select from another table if rows do or do not exist

    Hi,

    I hope I can explain this right:

    Here is my statement so far:
    [code=mysql]
    SELECT shift_log.note, shift_log.id, shift_log.times tamp, shift_log.monit or, shift_log.hando ver,
    shift_log.start _time, shift_log.finis h_time, shift_log.sunre f, shift_log.hpref , shift_log.hsbcr ef,
    CONCAT(users.fn ame, " ", users.lname) AS users_name, equipment.name AS equipment_name, equipment_id";
    FROM shift_log, equipment, users, part_usage
    WHERE equipment.id = shift_log.equip ment_id AND shift_log.user = users.id AND part_usage.shif t_log_id = shift_log.id
    ORDER BY shift_log.times tamp DESC
    [/code]

    The last AND statement (AND part_usage.shif t_log_id = shift_log.id) in the where clause is not always true, I want to get the info if its there, but allow the statement to run if there are no records, but im lost as to how to do so. As it is i only get results for those that have an entry in part_usage, but I still want the other rows that have no entry in part_usage. Is this possible?
    Last edited by Atli; Jul 26 '08, 11:14 PM. Reason: Added [code] tags.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Use a LEFT OUTER JOIN for that.

    Comment

    Working...