[英]mySQL - SELECT using JOIN, or a nested SELECT (sub-select)?
I am trying to write a SELECT in mySQL (and PHP) that will retrieve all the rows in "Images" table that were not ranked yet by a certain user. 我正在尝试在mySQL(和PHP)中编写SELECT,它将检索“ Images”表中尚未由特定用户排名的所有行。
Those are my tables: 这些是我的表:
Table: Images 表: 图像
+-----------+----------+-----------+----------+
| Index | Rank_Good| Rank_OK | Rank_Bad |
+-----------+----------+-----------+-----------
| 201 | 2 | 9 | 28 |
| 202 | 11 | 20 | 39 |
| 203 | 36 | 14 | 7 |
+-----------+----------+-----------+----------+
Table2: WhoAlreadyClickedImg (has no index) 表2: WhoAlreadyClickedImg (无索引)
+------------+-----------------+-----------+
| ImageIndex | UserWhoRankedIt | RankGiven |
+------------+-----------------+-----------+
| 202 | 87 | OK |
+------------+-----------------+-----------+
| 202 | 93 | Bad |
+------------+-----------------+-----------+
| 204 | 93 | Good |
+------------+-----------------+-----------+
| 203 | 94 | Bad |
+------------+-----------------+-----------+
Every time a user rank an image, the table "Images" is updated and a row is added to "WhoAlreadyClickedImg" table. 每次用户对图像进行排名时,表“ Images”都会更新,并将行添加到“ WhoAlreadyClickedImg”表中。 (this table has no index)
(该表没有索引)
for example, if the user ranked image index 202 with "ok", then the col "Rank_OK" will be updated to (+1) and then, a new row will be added to the "WhoAlreadyClickedImg" table: 例如,如果用户使用“ ok”对图像索引202进行排名,则col“ Rank_OK”将更新为(+1),然后将新行添加到“ WhoAlreadyClickedImg”表中:
ImageIndex: 201 | 图片索引:201 | UserWhoRankedIt: (the used session id) |
UserWhoRankedIt :(使用的会话ID) | RankGiven: OK
RankGiven:好的
i want to build a select that will not show the same image twice to a user who already ranked it. 我想建立一个选择,它将不会对已经对其进行排名的用户显示两次相同的图像。
for example, if I'm user "93", the only image that the select will bring is "203" 例如,如果我的用户是“ 93”,则选择将带来的唯一图像是“ 203”
UPDATED: 更新:
This is the query i'm using (by @eamonn): 这是我正在使用的查询(@eamonn提供):
SELECT * FROM Images WHERE Index NOT IN (SELECT ImageIndex FROM WhoAlreadyClickedImg WHERE UserWhoRankedIt = 93);
SELECT * FROM不在索引中的图像(SELECT ImageIndex从WhoAlreadyClickedImg WHERE UserWhoRankedIt = 93);
but I get an error: 但我得到一个错误:
You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Index NOT IN (SELECT ImageIndex FROM WhoAlreadyClickedImg WHERE UserWhoRankedIt' at line 1
检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“索引不输入(从WhoAlreadyClickedImg WHERE UserWhoRankedIt选择SELECT ImageIndex)附近使用
Storage Engine: InnoDB 存储引擎:InnoDB
MySQL db version: 5.5.33-29.3 MySQL db版本:5.5.33-29.3
both tables now have Index, int(11), defined as PRIMARY, auto_increment 这两个表现在都有索引int(11),定义为PRIMARY,auto_increment
maybe someone has an idea? 也许有人有主意?
SELECT * FROM Images WHERE Index NOT IN (SELECT ImageIndex FROM WhoAlreadyClickedImg WHERE UserWhoRankedIt = 93);
简单的子查询。
Well the column name Index is a reserved keyword in MySQL. 好吧,列名Index是MySQL中的保留关键字。 You should change the name of the Index column to Id or MainIndex.
您应该将索引列的名称更改为Id或MainIndex。 See this link: https://dev.mysql.com/doc/refman/5.6/en/keywords.html
看到这个链接: https : //dev.mysql.com/doc/refman/5.6/en/keywords.html
Another option is to use the table name inside the select query. 另一种选择是在选择查询中使用表名。 For example:
例如:
SELECT * FROM Images WHERE Images.Index NOT IN (SELECT ImageIndex FROM WhoAlreadyClickedImg WHERE UserWhoRankedIt = 93);
I think you should rename your column since that minimizes the chances of errors in SQL statements. 我认为您应该重命名列,因为这样可以最大程度地减少SQL语句中出现错误的机会。
There, this should work: 在那里,这应该工作:
SELECT Images.Index
FROM Images
WHERE Images.Index NOT IN
(
SELECT WhoAlreadyClickedImg.ImageIndex
FROM WhoAlreadyClickedImg
WHERE WhoAlreadyClickedImg.UserWhoRankedIt = 93
);
Remember to add the names of the tables before the name of the column. 切记在表名之前添加表名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.