简体   繁体   中英

How to show multiple values of a single field in a single row in a single query?

Table 1:Domain Link Result
======================================================================
||Column1(words)     ||Column2(links)    ||Column3(frequency)       ||
======================================================================
    1                   1                   Any Number
    2                   1                   Any Number
    3                   1                   Any Number
    4                   1                   Any Number
    1                   2                   Any Number
    2                   2                   Any Number
    3                   2                   Any Number
    4                   2                   Any Number


Table 2:Sub Link Result
======================================================================
||Column1(words)     ||Column2(sublinks)    ||Column3(frequency)    ||
======================================================================
    1                   a                   Any Number
    2                   b                   Any Number
    3                   c                   Any Number
    4                   d                   Any Number
    1                   e                   Any Number
    2                   f                   Any Number
    3                   g                   Any Number
    4                   h                   Any Number

And so on.

In the above scenario user entered 4 words and 2 domain links. Now the frequency of 4 keywords is calculated on domain links as well sublinks and stored in separate tables as shown above. I want an aggregate result like below:

Table 3:Final Result
==================================================================================
||Column1(words)     ||Column2(Domain links)    ||Column3(Total frequency)      ||
==================================================================================
Row1:    1              1                    Total of frequency in both tables
                        2                    for word "1"   
----------------------------------------------------------------------------------
Row2:    2              1                    Total of frequency in both tables
                        2                    for word "2"   
----------------------------------------------------------------------------------
Row3:    3              1                    Total of frequency in both tables
                        2                    for word "3"   
----------------------------------------------------------------------------------
Row4:    4              1                    Total of frequency in both tables
                        2                    for word "4"   
----------------------------------------------------------------------------------

I tried the following query in MySQL:

SELECT t.`keyword`, t.`link` SUM( t.`frequency` ) AS total
FROM (

SELECT `frequency`
FROM `domain_link_result`
WHERE `keyword` = 'national'
UNION ALL
SELECT `frequency`
FROM `sub_link_result`
WHERE `keyword` = 'national'
)t GROUP BY `keyword`

But in Column 2 of the final result I get only first link instead of two links for row 1. How can I get both links or any number of links entered by user in a single row ?

Words and Links have VARCHAR as type and frequency has INT type.

If you want to collapse several rows into one and still be able to see the information, you have to use GROUP_CONCAT

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

This outputs the collapsed values separated by commas, ie: a string. In your programming language you can split this string if you need individual values.

Your query would look somehow like this

SELECT keyword, GROUP_CONCAT(links), SUM(frequency)

FROM (subquery)

GROUP BY keyword

Which would output something like this:

==================================================================================
||Column1(words)     ||Column2(Domain links)    ||Column3(Total frequency)      ||
==================================================================================
Row1:    1              1,2                     sum of freq.
----------------------------------------------------------------------------------
Row2:    2              1,2                     sum of freq.
----------------------------------------------------------------------------------
Row3:    3              1,2                     sum of freq.
----------------------------------------------------------------------------------
Row4:    4              1,2                     sum of freq.

EDIT: Extra help for your query

Your query looks a little bit confusing to me. Try with a JOIN approach:

SELECT domain_link_results.word AS word,
       GROUP_CONCAT(domain_link_results.links) AS domain_links,
       domain_link_results.frequency + sub_link_results.frequency AS total_frequency

FROM domain_link_results

INNER JOIN sub_link_results
        ON domain_link_results.word = sub_link_results.word

WHERE domain_link_results.word = "national"

GROUP BY domain_link_results.word

On the other hand, it might be better to have all the links in the same table, and an extra field to determine if it's a domain link or a sublink. Without knowing more about your system it is hard to say.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM