I have created a basic website that has a form. This form captures the following information from a user and stores it in the PHPMyAdmin table:
First Name, Surname, Title, Description, Email Address, Display Email Address (Yes/No).
An example of input would be:
David, Beckham, Mr, Hello I am a boy, [email protected], Yes
Also on the website, I have a table that shows a list of all the users stored in my database through the use of this PHP code:
$sql = "SELECT UserID, UserForeName, UserSurname, UserTitle, UserDescription,
UserEmailAddress FROM Users";
How would I edit this SQL so that if a user answers “No” to the “Display Email” field, their row would omit their email address from the row. For example:
David, Beckham, Mr, Hello I am a boy, [email protected], Yes
Lucy, Sky, Mrs, Hello I am a girl, , No
I have tried the following query, but have had no luck so far:
SELECT CASE WHEN EmailReveal = 'yes'
THEN SELECT *
ELSE SELECT UserID, UserForeName, UserSurname, UserTitle, UserDescription
END AS col
FROM Users;
3
Answers
you can use
union
but for that you have to be same number of column so you could use ” blank string for making same number of column in both selectionThis is how you would use a CASE WHEN in a simple way for this.
Please try this query: