用于在复杂的mysql查询中提取列名和别名的PHP代码

时间:2012-06-06 12:05:58

标签: php regex mysqli

我在PHP程序中有一个select查询。

SELECT u.name AS name,
    u.email AS email,
    u.status AS status ,
    u.is_blocked AS is_blocked,         
    pm.plan_name AS plan_name,
    upt.amount AS amount,
    upt.payment_status AS payment_status,
    DATE_FORMAT(upt.dt, '%m-%d-%Y')AS payment_date,
    DATE_FORMAT(upt.plan_expiry_date,'%m-%d-%Y') AS exp_dt
FROM users u, user_plan_transaction upt, plan_master pm
WHERE
    u.user_id = upt.user_id
    AND upt.payment_status  IN ('PAID', 'ADMIN_PAID')
    AND upt.dt = (SELECT dt FROM user_plan_transaction WHERE user_id = u.user_id
                  ORDER BY dt DESC
                  LIMIT 1 )
    AND upt.plan_id = pm.plan_id

从这个查询我需要提取两个数据集:
(两个数据集都是列名,如下所示:)

(1) u.name, u.email, u.status, u.is_blocked, pm.plan_name, upt.amount, upt.payment_status, 
DATE_FORMAT(upt.dt, '%m-%d-%Y'),  DATE_FORMAT(upt.plan_expiry_date,'%m-%d-%Y')
(2) name, email, status, is_blocked, plan_name, amount, payment_status, payment_date, exp_dt

我尝试使用mysqli_fetch_field()函数执行此操作。 但该函数不会在数据集

中返回值DATE_FORMAT(upt.dt, '%m-%d-%Y')DATE_FORMAT(upt.plan_expiry_date,'%m-%d-%Y')

此外,如果查询稍后更改,我希望能够再次提取字段。

EG。查询2:

SELECT name,
       email,
       status,
       is_blocked
FROM users

然后我需要两个数据集:

(1) name, email, status, is_blocked
(2) name, email, status, is_blocked

我需要为任何mysql查询提取这两个数据集。

有人可以帮忙吗。

由于

我找不到基于mysql php的解决方案..我是否必须使用正则表达式来解决这个问题? 如何通过逗号(不在括号中)preg_split。例如:

DATE_FORMAT(upt.dt,'%m-%d-%Y')AS payment_date,

我不想在“upt.dt”之后用逗号分隔,而是在“payment_date”之后用逗号分隔。

0 个答案:

没有答案