MySQL: Returning primary key as null
SELECT pr.id as process_id,
lo.update_time as start_date,
REPLACE( sc.message, 'ip.ip.ip.ip', lo.variables ) as message,
sc.code
FROM logs AS lo
LEFT JOIN pr开发者_高级运维ocesses AS pr ON lo.process_id = pr.id
LEFT JOIN status_codes AS sc ON lo.status_code_id = sc.code
ORDER BY lo.id DESC
LIMIT 14
The issue with that query is that it's returning process_id as all NULL values, even though that's the auto-incremented primary key of that table without any NULL values.
Use SELECT lo.process_id
not SELECT pr.id as process_id
The Rows in which pr.id
is NULL
are the ones for which no record exists in the processes pr
table.
These are kept in because you are using a LEFT OUTER JOIN
rather than an INNER JOIN
You're using a LEFT JOIN. Therefore, for every row that exists in either the logs
or status_code
tables that doesn't have a corresponding row in the logs
table, you will get NULL
values for the corresponding "row" of processes
.
精彩评论