从数据库中抓取满足两个条件的所有用户
Grab all users from Database that meet two conditions
我正在尝试获取类型为 usertype 1
或 usertype 3
的所有用户。
我想我可以使用连接来完成,但我想要没有连接的东西。
这是我正在尝试的:
$users = User::model()->findAllBySql("SELECT * FROM user WHERE (usertype = 3 and usertype = 1)");
我用的是Yii不过应该是一样的SQL原理
User(id, username, usertype, name);
usertype
是 1、2 或 3。
您需要熟悉 SQL WHERE 子句
http://www.w3schools.com/sql/sql_where.asp
特别是正如@Gordon 所指出的,"IN" 部分
交替
如果要求找到同时属于第 2 类和第 3 类的用户,那么您可以尝试
select distinct(id, username, name) from user
where usertype = 2
AND id in (select id from user where usertype = 3)
但是这是假设 ID 不是您的 PK,您的 PK 是 id 和用户类型的组合
使用示例 table 的更多信息以及您希望从基于数据的查询中获得的信息将阐明问题
我想太多了。这要归功于 Gordon Linoff,他说 or
但我被蒙蔽了双眼所以无法理解。
解决方法如下:
$users = User::model()->findAllBySql("SELECT * FROM user WHERE (usertype = 3 or usertype = 1)");
之前没有用,因为没有用户类型 1 和用户类型 3。它是一个 or
另一个。
最后的查询应该可以找到您要查找的内容。剩下的就是树立榜样。
drop schema example;
create schema example;
use example;
create table user(
user_id int primary key,
name varchar(64)
);
create table type(
type_id int primary key,
type_name varchar(64)
);
create table user_type (
user_id int,
type_id int,
foreign key(user_id) references user(user_id),
foreign key(type_id) references type(type_id)
);
insert into user values (1, 'John');
insert into user values (2, 'Paul');
insert into user values (3, 'George');
insert into user values (4, 'Ringo');
insert into type values (10, 'Guitar');
insert into type values (20, 'Bass');
insert into type values (30, 'Drums');
insert into type values (40, 'Vocals');
insert into type values (50, 'Sitar');
insert into user_type values (1, 10);
insert into user_type values (1, 40);
insert into user_type values (2, 20);
insert into user_type values (2, 40);
insert into user_type values (3, 10);
insert into user_type values (3, 40);
insert into user_type values (3, 50);
insert into user_type values (4, 30);
insert into user_type values (4, 40);
select
u.*
from
user u
join user_type guitar on u.user_id = guitar.user_id and guitar.type_id = 10
join user_type sitar on u.user_id = sitar.user_id and sitar.type_id = 50
;
编辑:这是在 MySql 中完成的。此查询的结果集是一条记录:George 是唯一匹配吉他和西塔琴两种类型的用户。
你可以这样使用CActiveRecord findAll()
方法:
$users = User::model()->findAll(array('condition'=>'usertype = 3 OR usertype = 2'));
我正在尝试获取类型为 usertype 1
或 usertype 3
的所有用户。
我想我可以使用连接来完成,但我想要没有连接的东西。
这是我正在尝试的:
$users = User::model()->findAllBySql("SELECT * FROM user WHERE (usertype = 3 and usertype = 1)");
我用的是Yii不过应该是一样的SQL原理
User(id, username, usertype, name);
usertype
是 1、2 或 3。
您需要熟悉 SQL WHERE 子句
http://www.w3schools.com/sql/sql_where.asp
特别是正如@Gordon 所指出的,"IN" 部分
交替
如果要求找到同时属于第 2 类和第 3 类的用户,那么您可以尝试
select distinct(id, username, name) from user
where usertype = 2
AND id in (select id from user where usertype = 3)
但是这是假设 ID 不是您的 PK,您的 PK 是 id 和用户类型的组合
使用示例 table 的更多信息以及您希望从基于数据的查询中获得的信息将阐明问题
我想太多了。这要归功于 Gordon Linoff,他说 or
但我被蒙蔽了双眼所以无法理解。
解决方法如下:
$users = User::model()->findAllBySql("SELECT * FROM user WHERE (usertype = 3 or usertype = 1)");
之前没有用,因为没有用户类型 1 和用户类型 3。它是一个 or
另一个。
最后的查询应该可以找到您要查找的内容。剩下的就是树立榜样。
drop schema example;
create schema example;
use example;
create table user(
user_id int primary key,
name varchar(64)
);
create table type(
type_id int primary key,
type_name varchar(64)
);
create table user_type (
user_id int,
type_id int,
foreign key(user_id) references user(user_id),
foreign key(type_id) references type(type_id)
);
insert into user values (1, 'John');
insert into user values (2, 'Paul');
insert into user values (3, 'George');
insert into user values (4, 'Ringo');
insert into type values (10, 'Guitar');
insert into type values (20, 'Bass');
insert into type values (30, 'Drums');
insert into type values (40, 'Vocals');
insert into type values (50, 'Sitar');
insert into user_type values (1, 10);
insert into user_type values (1, 40);
insert into user_type values (2, 20);
insert into user_type values (2, 40);
insert into user_type values (3, 10);
insert into user_type values (3, 40);
insert into user_type values (3, 50);
insert into user_type values (4, 30);
insert into user_type values (4, 40);
select
u.*
from
user u
join user_type guitar on u.user_id = guitar.user_id and guitar.type_id = 10
join user_type sitar on u.user_id = sitar.user_id and sitar.type_id = 50
;
编辑:这是在 MySql 中完成的。此查询的结果集是一条记录:George 是唯一匹配吉他和西塔琴两种类型的用户。
你可以这样使用CActiveRecord findAll()
方法:
$users = User::model()->findAll(array('condition'=>'usertype = 3 OR usertype = 2'));