Matching Different Value within One Row

て烟熏妆下的殇ゞ 提交于 2020-01-06 02:14:35

问题


I want to perform a query whereby I want to check whether on the columns A has either certain values. A could have only X , X and Y or a combination of X Y and Z.

To give a better understanding. I am checking a book's author within a table itself. The table has the BOOK_ID , BOOK_TITLE , AUTHOR_NAME, AUTHOR_ORDER.

So a book might have 1,2 or 3 authors, listed in order written inside the AUTHOR_ORDER row. I am trying very hard to reach an output where if a book has 3 authors, it will display accordingly from the first author to the third author. I am now stuck in the part where I need to compare the value and present it in the output.

Any idea how to achieve this in MYSQL output?

Sample :

The output result is more or less like this:

If the title has au_ord of 1,2 and 3, there shall be a new column with all the authors name listed in ascending.

So for example, for title BU1032, the Author row will be Bennet, Green


回答1:


I think GROUP_CONCAT is what you are after:

SELECT  Title_ID, 
        Title,
        GROUP_CONCAT(au_LName ORDER BY au_Ord) AS Authors
FROM    Books
GROUP BY Title_ID, Title;

SQL FIDDLE



来源:https://stackoverflow.com/questions/13126011/matching-different-value-within-one-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!