[英]mysql alias for column name for insert update delete operation
Is it possible to have one column name but has 2 different names refer to it ? 是否可以有一个列名,但有2个不同的名称引用呢?
I have a table with column name "iduser". 我有一个列名为“ iduser”的表。 But I want to use some php plugin, and the plugin uses column name "id". 但是我想使用一些php插件,并且该插件使用列名“ id”。
So I wonder if there is a quick way in mysql to create alias "id" for the current columname "iduser". 因此,我想知道mysql中是否有一种快速方法为当前columname“ iduser”创建别名“ id”。 I wanted to do CRUD operations based on this. 我想基于此进行CRUD操作。
I do this because I'm lazy to change all the column name of the plugin tobe "iduser". 我这样做是因为我懒于将插件的所有列名称更改为“ iduser”。
Thx. 谢谢。
It's possible using a view . 可以使用view 。
Just make sure that your view definition meets the requirements at 19.5.3. 只要确保您的视图定义符合19.5.3的要求即可。 Updatable and Insertable Views . 可更新的和可插入的视图 。 But that shouldn't be a problem since you'd have the simplest 1:1 relationship using the primary key of the table. 但这不应该成为问题,因为使用表的主键将具有最简单的1:1关系。
How it will affect performance is up to you to measure... 它如何影响性能取决于您的衡量标准...
eg 例如
<?php
// this script creates a persistent table soFooTbl and a persistent view soFooView
// can't use temporary tables for this example
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);
printTable($pdo, 'soFooTbl');
printTable($pdo, 'soFooView');
echo "deleting rows\n";
$pdo->exec('DELETE FROM soFooView WHERE id IN (1,2,3,4,5)');
printTable($pdo, 'soFooTbl');
printTable($pdo, 'soFooView');
function printTable($pdo, $src) {
echo "---- $src ----\n";
foreach( $pdo->query('SELECT * FROM '.$src, PDO::FETCH_ASSOC) as $row ) {
echo join(', ', $row), " | ";
}
echo "\n";
}
function setup($pdo) {
$pdo->exec('
CREATE TABLE IF NOT EXISTS soFooTbl(
idUser int auto_increment,
val varchar(32),
primary key(idUser)
)
');
$pdo->exec('TRUNCATE TABLE soFooTbl');
$stmt = $pdo->prepare('INSERT INTO soFooTbl (val) VALUES (:val)');
$stmt->bindParam('val', $val);
foreach( range(1,10) as $val ) {
$stmt->execute();
}
$pdo->exec('
CREATE OR REPLACE VIEW
soFooView
(id,val)
AS
SELECT
idUser,val
FROM
soFooTbl
');
}
prints 版画
---- soFooTbl ----
1, 1 | 2, 2 | 3, 3 | 4, 4 | 5, 5 | 6, 6 | 7, 7 | 8, 8 | 9, 9 | 10, 10 |
---- soFooView ----
1, 1 | 2, 2 | 3, 3 | 4, 4 | 5, 5 | 6, 6 | 7, 7 | 8, 8 | 9, 9 | 10, 10 |
deleting rows
---- soFooTbl ----
6, 6 | 7, 7 | 8, 8 | 9, 9 | 10, 10 |
---- soFooView ----
6, 6 | 7, 7 | 8, 8 | 9, 9 | 10, 10 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.