简体   繁体   English

我可以在 SQL 查询中添加一些 PHP 代码吗?

[英]Can I add some PHP code into an SQL query?

I am currently running a script that pulls back transaction data (one line for each transaction) from a database with the following SQL script, which is being run in MySQL Workbench.我目前正在运行一个脚本,该脚本使用以下 SQL 脚本从数据库中提取事务数据(每个事务一行),该脚本在 MySQL Workbench 中运行。

SELECT 
id,
merchant_id, 
affiliate_id, 
date, 
sale_amount, 
commission, 
ip

FROM transactions.transaction201505  

One of the columns in the table t.transactions is IP address.表 t.transactions 中的一列是 IP 地址。 Is there a way to embed this PHP script (or a function to this effect) within an SQL script: php function geoip_country_name_by_addr http://php.net/manual/en/function.geoip-country-code-by-name.php有没有办法在 SQL 脚本中嵌入这个 PHP 脚本(或一个函数来实现这个效果): php function geoip_country_name_by_addr http://php.net/manual/en/function.geoip-country-code-by-name.php

I have seen many examples of adding MySQL to PHP but would like to essentially add in Country/ City of sale to my data the other way around, so that the result could look like the sample below, which could be run off a tool such as MySQL Workbench.我已经看到了许多将 MySQL 添加到 PHP 的示例,但我想基本上以相反的方式将销售国家/城市添加到我的数据中,以便结果看起来像下面的示例,可以使用诸如MySQL 工作台。 I don't have access to run PHP scripts on this database, and therefore need a solution with SQL.我无权在此数据库上运行 PHP 脚本,因此需要使用 SQL 的解决方案。

Thanks in advance for any help you can offer on this.在此先感谢您提供的任何帮助。

Jamie杰米

在此处输入图片说明

No, this is not directly possible.不,这不是直接可能的。 You could write a UDF (user define function) in an exteranl module (eg a .DLL, .so or .dylib) but using PHP in such a module is probably not possible either (because PHP is a scripting language and you would need to compile your UDF into binary code).您可以在外部模块(例如 .DLL、.so 或 .dylib)中编写 UDF(用户定义函数),但也可能无法在此类模块中使用 PHP(因为 PHP 是一种脚本语言,您需要将您的 UDF 编译为二进制代码)。 However, MySQL has a number of built-in functions ( https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html ).但是,MySQL 有许多内置函数( https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html )。 Even though not what you want here.即使这里不是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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