0

Cant figure out how to get this result:

ProductID       Variantno
53121           5197, 5198,5199

From this data collection.

ProductID       Variantno
53121           5197
53121           5198
53121           5199

Tried with group by but no good result, total sql noob...

1
  • which rdbms you are dealing with? mysql or sql-server? or you need a cross-rdbms solution? Commented Nov 17, 2014 at 11:02

4 Answers 4

2

Try this..

SELECT 
  ProductID,
  GROUP_CONCAT(Variantno)
FROM tbl
GROUP BY ProductID
Sign up to request clarification or add additional context in comments.

2 Comments

Consider that the result returned by GROUP_CONCAT is a string (just as a note)
Don't forget to Select as Answer :P
0
select ProductID , group_concat(Variantno)
from table
group by ProductID

Comments

0

Try this:

WITH cte AS (
    SELECT 
        ProductID,
        CAST('<r>' + REPLACE(variantNo, ',', '</r><r>') + '</r>' AS XML) AS VariantNos
    FROM TestTable
)
SELECT 
    ProductID,
    xTable.xColumn.value('.', 'VARCHAR(MAX)') AS VariantNo
FROM cte
CROSS APPLY VariantNos.nodes('//r') AS xTable(xColumn) 

Comments

0

Without showing your code and database. I assumed that you are working with php and mysql. I have run and tested this code and it works. that means it will work for you. my connection is PDO. Give me a shout if are still having issues

<?php



// pdo connection



$db = new PDO (

    'mysql:host=localhost;dbname=sectona_db;charset=utf8', 

    'root', // username



    'root90' // password

);

?>




                    <?php

Echo 'Data Output:<br>';
include("pdo.php");

$result = $db->prepare("SELECT * FROM product where id='123'");
        $result->execute(array());



while ($r = $result->fetch()) 
                       {
//$data = htmlentities($r['product'], ENT_QUOTES, "UTF-8");






?>

<?php echo htmlentities($r['product'], ENT_QUOTES, "UTF-8");?>,



<?php } ?>

Comments

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.