简体   繁体   English

这个mysql查询怎么了?

[英]What is wrong with this mysql query?

This is my mysql query: 这是我的mysql查询:

SELECT * 
FROM wp_bp_group_documents 
WHERE group_id = 4 
  AND id IN (2) 
  AND (file LIKE '%jpg' 
       OR file LIKE '%png' 
       OR file LIKE '%jpeg' 
       OR file LIKE '%gif')     
ORDER BY created_ts DESC LIMIT 0, 20

and this is my table structure: 这是我的表结构:

'id', 'user_id', 'group_id', 'created_ts', 'modified_ts', 'file', 'name', 'description', 'featured', 'download_count'
'1', '1', '1', '1301587327', '1301587327', '1301587327-NewApplicationForm.pdf', 'Tenancy application form', 'This is a tenancy application form. Please download and fill in.', '0', '0'
'2', '1', '4', '1301934439', '1302005432', '1301934439-bupa_large.png', 'BUPA Stand Image', 'asd', '0', '0'

So from this query I expect it to find the second record in the database, however it returns no values. 因此,从该查询中,我希望它在数据库中找到第二条记录,但是它不返回任何值。

There are no mysql errors. 没有mysql错误。 I'm just wondering if I've done the query right? 我只是想知道我是否正确执行了查询?

Thank you 谢谢

file is a MySQL keyword. file是一个MySQL关键字。 Try wrapping the file name in backquotes: ` file `. 尝试包裹file中的反引号名:` file `。

Your id column appears to be a string, but you're specifying an integer in the array. 您的id列似乎是一个字符串,但是您要在数组中指定一个整数。 Could that be the problem? 这可能是问题吗?

Slightly off the OP's topic, but you know (right?) that when you include "like" in any select, then MySQL has no choice but to read through the entire table . 稍微偏离了OP的主题,但是您知道(对吗?),当在任何选择中包括“ like”时,MySQL便只能通读整个表 To avoid that, some folks try to make sure that values like filetypes -- things that already, by definition, lend their own taxonomy -- are broken out as non-unique indices. 为了避免这种情况,有些人试图确保像文件类型这样的值(根据定义已经属于自己的分类标准的东西)被分解为非唯一索引。

If you decide to do that, your table suddenly becomes unnormalized. 如果您决定这样做,则表突然变得不规范。 To remedy that , the table needs another column, say `pictype`, that you'd index. 为了解决这个问题 ,该表还需要另外一列称为“ pictype”的索引。 To me, that's a stronger table design anyway, but it's just MHO. 对我而言,无论如何这是一个更强大的表格设计,但这只是MHO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM