简体   繁体   English

如何通过列/字段值选择MySQL表中的行?

[英]How do I select rows in a MySQL table by column/field value?

i have table called messages which has field called (emails) with type text . 我有一个名为messages的表,其中的字段名为(emails)的文本类型为。 for example this field value is (email@domain.com , example@any.net . notme@yahoo.com , me@yahoo.com). 例如,此字段值为(email@domain.com,example@any.net。notme@yahoo.com和me@yahoo.com)。 and what i want when i am logged in with (me@yahoo.com) to display only messages that has value of (me@yahoo.com). 以及当我使用(me@yahoo.com)登录以仅显示具有(me@yahoo.com)价值的消息时我想要什么。 how can it be done with mysql & php ? 如何用mysql&php完成?

SELECT * FROM MESSAGES WHERE EMAIL = 'me@yahoo.com';

but EMAIL should probably be indexed to avoid haveing to scan the whole table to find matching rows. 但应该为EMAIL建立索引,以避免必须扫描整个表以找到匹配的行。 And type TEXT is probably unnecessary. 并且可能不需要输入TEXT Emails are not likely to be longer than 100 characters, so maybe set the type to VARCHAR(100) or similar. 电子邮件的长度不可能超过100个字符,因此可以将类型设置为VARCHAR(100)或类似字符。

Or given that each user will have an id, it's perhaps better get them out by USER_ID (which should also be indexed): 或者考虑到每个用户都有一个ID,最好通过USER_ID(也应将其编入索引)将其删除:

SELECT * FROM MESSAGES WHERE USER_ID = ?;

where USER_ID is the ID in your USER table or equivalent. 其中USER_ID是USER表中的ID或等效名称。

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

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