Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Stuff' And 'FOR XML PATH' alternative in Mysql for the following stored prodedure of Ms-sql

I have the following query in Ms-Sql

  INSERT INTO tbl_web_price_update 

    Select bd_book_code,

    Case 
        When pd.bpd_price is null then  
            cast((a.bd_price*c.ptm_price) as numeric(10))   
        else
            cast((pd.bpd_price*c.ptm_price) as numeric(10))             
        end
    As Price

    from tbl_books_details a 
        inner join tbl_price_type_master c on a.bd_price_type = c.ptm_price_type_id 
        left join tbl_books_price_details pd on pd.bpd_book_code = a.bd_book_code 
    Where c.ptm_price_type_id = @price_type     

    SELECT distinct r.price, STUFF((SELECT distinct ','+ Cast(a.bd_book_code as varchar) FROM tbl_web_price_update a WHERE r.price = a.price FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '') FROM tbl_web_price_update r

I have migrated from ms-sql to my-sql and i am unable to write an alternative query in Mysql. Please Help.

like image 213
Ratna Avatar asked Dec 23 '13 06:12

Ratna


People also ask

What is for XML Path in SQL?

We can use FOR XML PATH to prepare a comma-separated string from the existing data. Let's create an Authors table and insert a few records into it. In the data, we can see we have an ID column and the AuthorName column. If we just select the records, it gives the output in the following format.

Can we use XML and MySQL together?

The most common way to store XML in MySQL is to use the LOAD_FILE() function to open an entire XML document, store it in a variable, and insert the variable into a table column.

Which of the following modes do you specify in a FOR XML clause?

In a FOR XML clause, you specify one of these modes: RAW. AUTO. EXPLICIT.

Which version of MySQL has the load XML option?

MySQL :: MySQL 8.0 Reference Manual :: 13.2. 8 LOAD XML Statement.


1 Answers

It looks like you're looking for something like this

SELECT price, GROUP_CONCAT(DISTINCT bd_book_code) bd_book_code
  FROM tbl_web_price_update
 GROUP BY price

Here is SQLFiddle demo

  • GROUP_CONCAT() function.
like image 151
peterm Avatar answered Nov 05 '22 07:11

peterm