Reputation: 629
I have 2 tables, names
and phones
I did this for the query
$result = mysqli_query($mysqli, "SELECT * FROM names ORDER BY fname ASC
RIGHT JOIN phones ON phones.id=names.phone_id"
);
I got $result
as false. My names table has a column named phone_id and it's a PK of phones
's id, like so
names
- phone_id (FK)
phones
- id (PK)
What's wrong with my sql above?
Upvotes: 0
Views: 33
Reputation: 6297
You should use ORDER BY
as last clause of your query. Becasue order by i.e. sorting works at last after fetch.
SELECT *
FROM names as n
RIGHT JOIN phones as p ON p.id=n.phone_id
ORDER BY fname ASC
To see more abour ORDER BY you can check the manual link
Upvotes: 0
Reputation: 1787
The syntax should go like this:
SELECT *
FROM names
RIGHT JOIN phones ON phones.id = names.phone_id
ORDER BY fname ASC
The ORDER BY
had to be moved to the end.
Upvotes: 2
Reputation: 93694
Order by
should be the last part of your query
SELECT *
FROM names n
RIGHT JOIN phones p ON p.id=n.phone_id
ORDER BY fname ASC
Start using alias
names to make the query more readable
Upvotes: 1