我有以下查询,该查询返回44行:
SELECT id, IF(r.make, CONCAT(makes.description, ': ', r.make), 0) AS make, IF(r.model, models.description, 0) AS model, r.text, r.h1_tag, r.title, r.keywords, r.description, r.website_search_path_id, r.website_vehicle_type_id
FROM website_results_text r
LEFT JOIN vehicle_makes makes ON makes.code = r.make
LEFT JOIN vehicle_models models ON models.code = r.model AND models.make = r.make
WHERE r.website_id = 1966
结果类似于以下内容,不包括并不重要的所有行和排除的列。
---------------------------------------------------------------------
| id | make | model | text | h1_tag | title |
_____________________________________________________________________
| 192| 0 | 0 | test | test | test |
| 193| Fiat:24 | 0 | test | test | test |
---------------------------------------------------------------------
下面是到目前为止的查询,您可以看到如果vehicle_makes
表中没有返回make,我将丢失IF语句。
$resultsText = ResultsText::where([ 'website_id' => $website->id ])
->join('vehicle_makes', 'website_results_text.make', '=', 'vehicle_makes.code')
->join('vehicle_models', 'website_results_text.model', '=', 'vehicle_models.code')
->select(
'vehicle_makes.description AS make',
'vehicle_models.description AS model',
'website_results_text.text',
'website_results_text.h1_tag',
'website_results_text.title',
'website_results_text.keywords',
'website_results_text.website_search_path_id',
'website_results_text.website_vehicle_type_id'
)->get();