如何在PHP的MySQL查询中排除含指定短语的描述结果?
Hey there! I see you want to filter out any project entries where the description contains "cheap" or "lowest price". Let's adjust your SQL query to add that exclusion logic.
Modified Query Explanation
We'll add NOT LIKE conditions to your WHERE clause, using SQL wildcard % to match any text before or after the target phrases. This ensures we exclude any row where the description includes those phrases anywhere in the text.
Updated PHP Code
<?php $query = "SELECT * FROM project_data WHERE budget='200' AND description NOT LIKE '%cheap%' AND description NOT LIKE '%lowest price%' ORDER BY id DESC LIMIT 0, 100"; $database->setQuery( $query ); $rows = $database->loadObjectList(); ?> <?php foreach( $rows as $row ){?> <?=$row->budget?> <?=$row->title?> <?=$row->description?> <?php }?>
Extra Tip for Case Insensitivity
If you need to exclude variations like "Cheap", "CHEAP", or "Lowest Price" (different capitalization), you can make the check case-insensitive. For MySQL, use the LOWER() function to standardize the text before checking:
AND LOWER(description) NOT LIKE '%cheap%' AND LOWER(description) NOT LIKE '%lowest price%'
This converts the description to lowercase first, so any case variation of the target phrases will be excluded.
内容的提问来源于stack exchange,提问作者mwz

