7

For example, if queue_watch shows a 0, I want the actual mysql output to say No, else say Yes. Changing the scheme is not an option.

SELECT user.user_name, queue.queue_name, queue_access.queue_access,
    queue_access.queue_watch
FROM queue_access
INNER JOIN user ON user.user_id = queue_access.user_id
INNER JOIN queue ON queue.queue_id = queue_access.queue_id
WHERE queue_access <> '' OR queue_watch = 1
ORDER BY user_name;

The SQL works for what I need, but I like to know if changing the output using SQL rather than PHP is possible. Also, is it possible to group all the queue_names in one row to each user_name so I don't have to have 153 rows returned.

Thanks

3
  • "Also, is it possible to group..." Please look up GROUP BY in the manual. You may also want to look at GROUP_CONCAT. And please don't ask two completely unrelated questions in one question. Commented May 22, 2012 at 20:34
  • I know sorry for hijacking the two part question but since it was related to the same query I thought might as well. I did think of GROUP BY but the problem is group by returns one user and one queue name, since one user can belong to many queues - so want it in a kind of list, but don't know if its possible Commented May 22, 2012 at 20:58
  • sorry i figured it out, group by on the bottom, with group_concat in the select did it... Commented May 22, 2012 at 21:11

2 Answers 2

10

It not an over head. It is better to be done in the query, I feel.

Try this -

SELECT user.user_name, queue.queue_name, queue_access.queue_access,
       IF(queue_access.queue_watch = 0, 'No', 'Yes')
FROM queue_access
INNER JOIN user ON user.user_id = queue_access.user_id
INNER JOIN queue ON queue.queue_id = queue_access.queue_id
WHERE queue_access <> '' OR queue_watch = 1
ORDER BY user_name;
Sign up to request clarification or add additional context in comments.

1 Comment

This one actually solved what I needed, (I saw it first honestly), and I just added the as to rename the column. Thanks
6

You can use the standard CASE expression. There are two slightly different ways to write a CASE expression.

Simple case expression:

SELECT
    CASE queue_access.queue_watch
        WHEN 0 THEN 'No'
        ELSE 'Yes'
    END AS your_alias,
    ...

Searched case expression:

SELECT
    CASE WHEN queue_access.queue_watch = 0 THEN 'No' ELSE 'Yes' END AS your_alias,
    ...

Or you could use the MySQL specific IF construct:

SELECT
    IF(queue_access.queue_watch, 'Yes', 'No') AS your_alias,
    ...

You may want to consider what should happen if your value is something other than 0 or 1.

7 Comments

@MarcusAdams: Sorry I was actually going to add that. I was just was testing my answer in SQL Fiddle just to make sure everything was OK, but to my surprise it didn't work there. Wasn't sure if I had mis-remembered the syntax, mistyped something, or if there was a bug in SQL Fiddle. Seems like it's a bug in SQL Fiddle.
Nice job showing the ANSI standard way with CASE as well as more succinct MySQL IF method.
I like the case..as method, but where do I put the case, I get sql error no matter where i plop it into the query.
@MarkByers The bug is due to the column name having a comma in it. Adding a alias (sans-comma) works around it: sqlfiddle.com/#!2/d41d8/775
@MarkByers I've fixed this bug, now. sqlfiddle.com/#!2/d41d8/711 Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.