我有这两个表:
表1
Username Password
aswilliams40 pantsftw
liamvdheuvel qwascoolzuiop
michaelsmith01 ilovejessica
rzajac toocoolforscool
pooh_sweety_70 legendarymythic
表2
SELECT Username FROM Table1, Table2
WHERE Table1.Username = Table2.Username AND
Table2.Password LIKE '%cool%' AND
Table1.Rate IS NOT NULL AND
Table1.Opinion = 'Negative';
我想找到密码中包含' cool'在它的某个地方,如果用户对项目进行评级,那么该用户也必须始终对项目留下负面意见。未评级的项目无所谓。
我尝试将查询放在一起,但它确实不起作用:/
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
android {
buildToolsVersion '23.0.0'
}
dependencies {
}
所以基本上我希望唯一的输出是' liamvdheuvel'
答案 0 :(得分:1)
gc.RgbFgColor = new Gdk.Color(204,204,204);
为了优化,你需要从table1中删除用户名(给这个表一个正确的名称,并将userID添加到table2(也正确命名)。)
SELECT DISTINCT Table1.Username
FROM Table1
LEFT JOIN Table2
ON table1.Username=table2.Username
Where
Table2.Password LIKE '%cool%' AND
Table1.Rate IS NOT NULL AND
Table1.Opinion = 'Negative';
表1
Table2
------
UserId **Username** **Password**
1 aswilliams40 pantsftw
2 liamvdheuvel qwascoolzuiop
3 michaelsmith01 ilovejessica
4 rzajac toocoolforscool
5 pooh_sweety_70 legendarymythic
答案 1 :(得分:1)
SELECT Table2.Username
FROM Table1,
Table2
WHERE Table1.Username = Table2.Username
AND Table2.Password LIKE '%cool%'
AND Table1.Rate IS NOT NULL
AND Table1.Opinion = 'Negative';
问题是您没有指定要获取“用户名”的表格。
如果您不想重复使用用户名,请添加DISTINCT
:
SELECT DISTINCT Table2.Username
FROM Table1,
Table2
WHERE Table1.Username = Table2.Username
AND Table2.Password LIKE '%cool%'
AND Table1.Rate IS NOT NULL
AND Table1.Opinion = 'Negative';
如果您不希望用户有负面和正面意见,请尝试以下操作:
SELECT DISTINCT Table2.Username
FROM Table1,
Table2
WHERE Table1.Username = Table2.Username
AND Table2.Password LIKE '%cool%'
AND Table1.Rate IS NOT NULL
AND Table1.Opinion = 'Negative'
AND Table2.Username NOT IN (SELECT Table1.Username
FROM Table1
WHERE Table1.Opinion = 'Positive');