我有以下选择查询。
$this->db->select("$order_table.user_id, $order_table.total_price,
$order_table.shipping, $order_table.tax,
$order_table.filled, $order_table.shipped,
$order_table.tracking");
$this->db->select("$user_table.first_name as user_first_name,
$user_table.last_name as user_last_name,
$user_table.email as user_email");
$this->db->select("$mailing_address_table.first_name as mailing_first_name,
$mailing_address_table.last_name as mailing_last_name,
$mailing_address_table.address as mailing_address,
$mailing_address_table.address_2 as mailing_address_2,
$mailing_address_table.city as mailing_city,
$mailing_address_table.state as mailing_state,
$mailing_address_table.zip_code as mailing_zip_code,
$mailing_address_table.country as mailing_country");
$this->db->select("$billing_address_table.first_name as billing_first_name,
$billing_address_table.last_name as billing_last_name,
$billing_address_table.address as billing_address,
$billing_address_table.address_2 as billing_address_2,
$billing_address_table.city as billing_city,
$billing_address_table.state as billing_state,
$billing_address_table.zip_code as billing_zip_code,
$billing_address_table.country as billing_country");
$this->db->join($user_table,
"$user_table.id = $order_table.user_id", "left");
$this->db->join($mailing_address_table,
"$mailing_address_table.user_id = $user_table.id", "left");
$this->db->join($billing_address_table,
"$billing_address_table.user_id = $user_table.id", "left");
无论如何,我想是使用CONCAT,我可以使用前缀"计费_"到我在结算中选择的每一列。当我没有选择所有内容,而只是选择特定的列时,我似乎无法解决这个问题。
我喜欢从$ user_table中选择的每一列都是" user _",$ mailing_table" mailing _"等等。
注意:
1)我没有选择这些选择查询中每列的所有字段,因此我无法进行SELECT * FROM ...
2)我不想更新数据库中的列名,只是为了这个特定的SELECT使用。
答案 0 :(得分:0)
您可以在此处创建字段列表:
SET @SCHEMA = 'toimport';
SET @PREFIX = 'billing_';
SET @POSTFIX = 'address';
SELECT GROUP_CONCAT(
CONCAT(@SCHEMA,'.',@PREFIX,'.',@POSTFIX,'.',COLUMN_NAME,
' AS ',@PREFIX,COLUMN_NAME) SEPARATOR ',\n')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @SCHEMA AND TABLE_NAME = CONCAT( @PREFIX, @POSTFIX );
结果看起来像:
toimport.billing_.address.id AS billing_id,
toimport.billing_.address.name AS billing_name,
toimport.billing_.address.street AS billing_street,
toimport.billing_.address.a1 AS billing_a1,
toimport.billing_.address.a2 AS billing_a2
答案 1 :(得分:0)
在这里您的答案更多表:
查询:
SET @SCHEMA = 'toimport';
SELECT
GROUP_CONCAT(
CONCAT(
@SCHEMA,'.',
SUBSTRING_INDEX(TABLE_NAME, '_', 1),'.',
SUBSTRING_INDEX(TABLE_NAME, '_', -1),'.',
COLUMN_NAME, ' AS ',
SUBSTRING_INDEX(TABLE_NAME, '_', 1),'_',COLUMN_NAME
) SEPARATOR ',\n'
)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @SCHEMA
AND TABLE_NAME IN ('billing_address', 'email_post');
结果如下:
toimport.billing.address.id AS billing_id,
toimport.billing.address.name AS billing_name,
toimport.billing.address.street AS billing_street,
toimport.billing.address.a1 AS billing_a1,
toimport.billing.address.a2 AS billing_a2,
toimport.email.post.id AS email_id,
toimport.email.post.name AS email_name,
toimport.email.post.street AS email_street,
toimport.email.post.a1 AS email_a1,
toimport.email.post.a2 AS email_a2