SELECT user.login, book.name
FROM user
LEFT JOIN book ON
user.login = book.author
WHERE user.login = ‘peter’
Now i get:
peter book1
peter book2
peter book2
Bu i wish get:
peter book1
book2
book2
Database: MySQL
Thanks
I agree with others that while this is something that can be performed in SQL, it really shouldn’t be.
The following assumes that there can be only one author per book, which is not the case in reality:
It uses a variable (only MySQL supports this, to my knowledge) to store the author/login value. The CASE expression is set to return the column value if it does not match what is currently stored in the variable. If the variable and the column value match, a zero length string will occupy the column value – you could change this to be NULL if you like.
I included an ORDER BY in the likelihood you’d want this to happen for multiple names, because there’s no way to guarantee data order without an ORDER BY.
I had to use a subquery, otherwise, the column where the variable gets set would appear in your resultset.