Context
Incorect results. This query returns all book titles as null.
SELECT * FROM books b JOIN authors a USING (author_id)
Antipattern
Using SELECT * FROM PHP database extension returns each row from the SQL query as an associative array. Most authors in the database has no title, so the result $row[title] is null. The examples in the book use wildcards to save space and to avoid distracting from the more interesting parts.Solution
Use alias
SELECT b.title, a.title AS salutation FROM books b JOIN authors a USING (author_id);
Software developers dont seems to like to type, which in a way makes their choice of career ironic.
Also, the convenience of using wildcards in queries can affect performance and scalability.
Always spell out the columns you need, instead of relying on wildcars.
Questions and answers:
Clink on Option to Answer
1. Using wildcard * in SQL ...
- a) leads to errors
- b) is quick
2. PHP db extension
- a) returns associative arrays
- b) can't use alias