如何通过 PHP 分隔 SQL 联合上的值

发布于 2024-12-12 04:23:53 字数 891 浏览 0 评论 0原文

我使用 mysql_fetch_array() 来执行此操作,并通过 phpMyAdmin 测试我的查询。而且,正如我认为它们都在同一行上一样,是否可以使用 mysql fetch array() 来做到这一点?

这是我的代码:

$sql="SELECT xml FROM g2s_api_logs WHERE functioncode='MQX' AND cardnumber= '$cardnumber' AND        
            direction='out' AND date(datetime)='$date' AND time(datetime)='$time' UNION SELECT xml FROM 
            g2s_api_logs WHERE functioncode='MQX' AND cardnumber= '$cardnumber' AND direction='in' AND 
            date(datetime)='$date' AND time(datetime)='$time' ";
            $query= mysql_query($sql)or die(mysql_error().$sql);
            $array=mysql_fetch_array($query,MYSQL_ASSOC);
            $result= htmlspecialchars($array['?']);
            $response=htmlspecialchars($array['?']);
            mysql_close();

请忽略问号;我还不知道要放什么进去。我使用 phpMyAdmin 运行这个,它起作用了。不过我想在我的显示中分开他们的结果。即使他们在同一排我也可以这样做吗?有没有 fetch 列之类的东西?

I'm using mysql_fetch_array() to do this, and testing my queries through phpMyAdmin. And, as I thought they are both on the same row, is it possible to do this using mysql fetch array()?

Here is my code:

$sql="SELECT xml FROM g2s_api_logs WHERE functioncode='MQX' AND cardnumber= '$cardnumber' AND        
            direction='out' AND date(datetime)='$date' AND time(datetime)='$time' UNION SELECT xml FROM 
            g2s_api_logs WHERE functioncode='MQX' AND cardnumber= '$cardnumber' AND direction='in' AND 
            date(datetime)='$date' AND time(datetime)='$time' ";
            $query= mysql_query($sql)or die(mysql_error().$sql);
            $array=mysql_fetch_array($query,MYSQL_ASSOC);
            $result= htmlspecialchars($array['?']);
            $response=htmlspecialchars($array['?']);
            mysql_close();

Please ignore the question mark; I dunno what to put into it yet. I run this one using phpMyAdmin and it worked. However I would like to separate their results in my display. Can I do that even if they are in the same row? Is there such a thing as fetch column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮了一弯 2024-12-19 04:23:53

如果您有这样的查询:

select * from X where ...
union
select * from Y where ...

并且您想知道特定行是否来自 X 或 Y,那么您可以向 SELECT 子句添加文字值来区分它们:

select 'X' as came_from, * from X where ...
union all
select 'Y' as came_from, * from Y where ...

只要列名/别名和类型与UNION 会对这种事情感到高兴。

If you have a query like this:

select * from X where ...
union
select * from Y where ...

and you want to know if a particular row came from X or Y, then you can add a literal value to the SELECT clause to differentiate them:

select 'X' as came_from, * from X where ...
union all
select 'Y' as came_from, * from Y where ...

as long as the column names/aliases and types match the UNION will be happy with this sort of thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文