答案 0 :(得分:7)
$items = \app\models\Feedback::find()
->with(['user' => function($q) {
$q->select(['id_user', 'username']);
}])
->select(['id_feedback', 'feedback'])
->where(['REPLAY_TO_ID'=>$ID_KOMENTAR])
->asArray()
->all();
答案 1 :(得分:3)
根据您尝试存储的数据的详细信息,为您提供两个建议,一个或两个可能有效:
查看ENUM或SET约束是否适合您;请注意,它们根据SQL模式提供不同级别的强制执行(例如,严格)。查看更多信息:
http://dev.mysql.com/doc/refman/5.0/en/constraint-invalid-data.html
AND / OR
实施INSERT,UPDATE和DELETE触发器来控制对数据的访问(您可能希望在创建数据之前最初填充数据;再次,这取决于您的方案)
答案 2 :(得分:2)
最简单的方法是创建一个只有一个合法值的列,然后将其声明为not null unique
。
这会强制表不超过一行,但仍允许零行。如果您需要阻止删除唯一的行,请使用不同答案中给出的触发方法。
create table example(
enforce_one_row enum('only') not null unique default 'only',
... your columns ....
);
答案 3 :(得分:1)
试试这个:
CREATE TABLE foo (x INT NOT NULL PRIMARY KEY CHECK (x = 1), col1 INT NOT NULL, col2 INT NOT NULL);
答案 4 :(得分:0)
您可以使用表权限(假设您已设置阻止插入/删除的相关权限,并且您不应再更新权限,但是,任何用户都有权限可以再次覆盖权限)
首先插入一对一记录 然后通过列table_priv
中的禁用INSERT, UPDATE, DROP
添加权限
mysql> desc tables_priv; +-------------+-------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-------+ | Host | char(60) | NO | PRI | | | | Db | char(64) | NO | PRI | | | | User | char(16) | NO | PRI | | | | Table_name | char(64) | NO | PRI | | | | Grantor | char(77) | NO | MUL | | | | Timestamp | timestamp | NO | | CURRENT_TIMESTAMP | | | Table_priv | set('Select','Insert','Update','Delete','Create','Drop','Grant','References','Index','Alter','Create View','Show view') | NO | | | | | Column_priv | set('Select','Insert','Update','References') | NO | | | | +-------------+-------------------------------------------------------------------------------------------------------------------------+------+-----+-------------------+-------+ 8 rows in set (0.00 sec)
答案 5 :(得分:0)
如果您的数据库设置为严格的'模式,你可以创建一个像这样的表:
create table foo(id enum(' SYSROW')not null,(其他列定义)主键(id));
答案 6 :(得分:0)
create table if not exists myTable ( ID int not null ) engine=innodb; select 'create trigger tbi_myTable'; drop trigger if exists tbi_myTable; delimiter // create trigger tbi_myTable before insert on myTable for each row begin if (select count(1) from myTable) > 0 then -- Signal is only in 5.6 and above use another way to raise an error: if less than 5.6 SIGNAL SQLSTATE '50000' SET MESSAGE_TEXT = 'Cannot insert into myTable only one row alowed!'; end if; END // delimiter ; insert into myTable values (1); -- This will generate an error insert into myTable values (2); -- This will only have one row with "1" select * from myTable;
答案 7 :(得分:-1)
您可以使用IF语句检查mySQL。
IF (SELECT count(*) FROM myTable) = 1
//your SQL code here.