Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Function in a SQL query. Is it possible?

So, in the previous question I asked, previous question

and didn't get any answer, so I have an idea from the previous question of mine, to make it more compact, so this what I thought, is it possible to call a function in a SQL query?

So this is the condition I had now, I have a function to convert price using API, and I want to sort it by price but the problem is each product have different currency.

Ex:

Product A - USD 1,800

Product B - IDR 1,900,000

Product C - EUR 1,000

So if I just sort by the price from the database, it'll messed up, because the currency are different. So I want to make the all the data from mysql is automatically changed to price in USD (default, here is when the function needed)

$args=array(
 'meta_key' => 'monthly',
 'meta_value' => 'yes',
 'posts_per_page' => 12,
 'paged' => $paged
);
$mam_global_fields = ', price.meta_value'; // I want to change this 
$mam_global_join = " 
 INNER JOIN " . $wpdb->postmeta . " AS currency ON (" . $wpdb->posts . ".ID = currency.post_id AND currency.meta_key = 'monthly_currency')
 INNER JOIN " . $wpdb->postmeta . " AS price ON (" . $wpdb->posts . ".ID = price.post_id AND price.meta_key = 'monthly_price')
";
mam_global_orderby="total_price ASC"; 

...To something like this, but I don't know how to do this, but it's something like this or some way similar like this or my previous question

$mam_global_fields = ', price.meta_value, '. convert_price('price.meta_value', 'currency.meta_value') . ' AS total_price';

I appreciate any kind of helps or ideas

Answer*

Draft saved
Draft discarded
Cancel
8
  • I will really appreciate it if you really willing to write me the function Commented Mar 31, 2018 at 12:12
  • @KhrisnaGunanasurya- The link below gives sql query for currency conversion.Lot of sql works needs to be done stackoverflow.com/questions/39882140/… Commented Mar 31, 2018 at 12:23
  • 1
    krishna, you will thank us someday IF YOU WRITE your own function. just do it. Commented Mar 31, 2018 at 12:28
  • @âńōŋŷXmoůŜ well I don't asking for someone to write the code, it's him the one that offering to write the function for me. Like I said, I appreciate any kind of helps or ideas. So if someone wants to write the codes for me, of course I'll appreciate it. Commented Mar 31, 2018 at 12:40
  • 1
    I have seen that you tagged mysql. I work with oracle sql and write stored procedures in pl/sql. I don worked with mysql. Commented Mar 31, 2018 at 13:34

default

AltStyle によって変換されたページ (->オリジナル) /