2014-12-01 100 views
0

我希望有人能幫助我。MySQL的IF語句中左連接

我有以下查詢

SELECT * FROM網格LEFT OUTER JOIN grid_images ON g_img_grid_id = G_ID & &((g_img_main IS NOT NULL)||(g_img_main IS NULL & & g_img_width> = 475 )),其中g_blog_id IS NOT NULL GROUP BY G_ID ORDER BY DESC G_ID

它不工作,我怎麼想它。我想要發生的是這個。在grid_images的左連接中,我想用g_img_main選擇一個圖像,如果有一個圖像不爲null,如果它們爲null,那麼我希望它選擇基於寬度大於474的圖像。so g_img_width> = 475.

有沒有辦法做到這一點?

爲了進一步說明我有兩個表。一個是「網格」,另一個是「grid_images」。所以每個網格都可以有無限的圖像。網格有一個id「G_ID」,並且每個圖像連接到「G_ID」 ......所以在「grid_images」「g_img_grid_id」告訴我,像屬於哪個網格。一些電網將有一個主圖像標示出所有的圖像「g_img_main」的,有些網格不會。在左邊的jin中,我想選擇標記爲主「g_img_main」的圖像(如果有的話),如果不是,我只想選取任何大於特定寬度「g_img_width」的圖像。

== Table structure for table grids 

|------ 
|Column|Type|Null|Default 
|------ 
|//**g_id**//|int(11)|No| 
|**g_permalink**|varchar(255)|No| 
|g_type|tinyint(3)|No|3 
|g_function|tinyint(3)|No|1 
|g_format|tinyint(3)|No|1 
|g_category|int(5)|No| 
|g_name|varchar(255)|No| 
|g_company_name|varchar(255)|Yes|NULL 
|g_owner_id|int(11)|No| 
|g_settings|text|No| 
|g_about|text|No| 
|g_website|varchar(2083)|No| 
|g_donate_link|varchar(2083)|Yes|NULL 
|g_donate_paypal|text|Yes|NULL 
|g_image|varchar(255)|Yes|NULL 
|g_view_permission|tinyint(1)|No|1 
|g_post_permission|tinyint(1)|No|1 
|g_allow_voting|tinyint(1)|Yes|NULL 
|g_show_share|tinyint(4)|Yes|NULL 
|g_site_grid|tinyint(1)|Yes|0 
|g_date_created|int(11)|No| 
|g_votes_per_user|int(11)|Yes|NULL 
|g_votes_require_email|tinyint(1)|Yes|NULL 
|g_facebook_eligible|tinyint(1)|Yes|NULL 
|g_facebook_active|tinyint(1)|Yes|NULL 
|g_facebook_app_tab_id|varchar(100)|Yes|NULL 
|g_facebook_page_id|varchar(100)|Yes|NULL 
|g_facebook_page_name|varchar(255)|Yes|NULL 
|g_facebook_page_category|varchar(100)|Yes|NULL 
|g_facebook_page_link|text|No| 
|g_facebook_page_about|text|No| 
|g_votes_rank_display|tinyint(1)|Yes|NULL 
|g_lock_center|tinyint(1)|Yes|NULL 
|g_passthe_item|varchar(255)|Yes|NULL 
|g_facebook_img|varchar(255)|Yes|NULL 
|g_pinterest_img|varchar(255)|Yes|NULL 
|g_montage_img|varchar(255)|Yes|NULL 
|g_views|int(11)|No|0 
|g_blog_id|int(11)|Yes|NULL 
== Dumping data for table grids 

|------ 
|g_id|g_permalink|g_type|g_function|g_format|g_category|g_name|g_company_name|g_owner_id|g_settings|g_about|g_website|g_donate_link|g_donate_paypal|g_image|g_view_permission|g_post_permission|g_allow_voting|g_show_share|g_site_grid|g_date_created|g_votes_per_user|g_votes_require_email|g_facebook_eligible|g_facebook_active|g_facebook_app_tab_id|g_facebook_page_id|g_facebook_page_name|g_facebook_page_category|g_facebook_page_link|g_facebook_page_about|g_votes_rank_display|g_lock_center|g_passthe_item|g_facebook_img|g_pinterest_img|g_montage_img|g_views|g_blog_id 
|------ 
|333|33-plus-ugliest-christmas-sweaters|1|1|4|0|33+ Ugliest Christmas Sweaters|NULL|11|null|<p><span style="line-height: 22.3999996185303px;">Just when you thought you've seen the ugliest Christmas sweaters around these people took ugly to the next level. Please feel free to share your comments, cuz some of these really need commenting...</span><br></p>| | |NULL| |1|2|NULL|1|1|1416865122|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL| | |NULL|NULL|NULL|2014/11/26/14170476241649711529-4x3.jpg|2014/11/26/14170476241649711529pin-2x6.jpg|2014/11/26/14170476241649711529.jpg|0|20 


== Table structure for table grid_images 

|------ 
|Column|Type|Null|Default 
|------ 
|//**g_img_id**//|int(11)|No| 
|g_img_mem_id|int(11)|Yes|1 
|g_img_dir|varchar(50)|No| 
|g_img_file|varchar(100)|No| 
|g_img_caption|text|Yes|NULL 
|g_img_link|varchar(2083)|No| 
|g_img_grid_id|int(11)|No|1 
|g_img_date_added|int(11)|No| 
|g_img_uploader_ip|int(11)|No| 
|g_img_lat|decimal(18,12)|Yes|NULL 
|g_img_lng|decimal(18,12)|Yes|NULL 
|g_img_width|int(6)|Yes|NULL 
|g_img_height|int(6)|Yes|NULL 
|g_img_reported|tinyint(1)|Yes|NULL 
|g_img_reported_access_code|varchar(100)|Yes|NULL 
|g_img_last_edit|int(11)|Yes|NULL 
|g_img_flickr_id|varchar(255)|No| 
|g_img_facebook_id|varchar(255)|No| 
|g_img_original_id|int(11)|Yes|NULL 
|g_img_reposted_from|int(11)|Yes|NULL 
|g_img_first_name|varchar(100)|Yes|NULL 
|g_img_email|varchar(255)|Yes|NULL 
|g_img_hearts|int(11)|No|0 
|g_img_views|int(11)|No|0 
|g_img_main|tinyint(1)|Yes|NULL 
== Dumping data for table grid_images 

|------ 
|g_img_id|g_img_mem_id|g_img_dir|g_img_file|g_img_caption|g_img_link|g_img_grid_id|g_img_date_added|g_img_uploader_ip|g_img_lat|g_img_lng|g_img_width|g_img_height|g_img_reported|g_img_reported_access_code|g_img_last_edit|g_img_flickr_id|g_img_facebook_id|g_img_original_id|g_img_reposted_from|g_img_first_name|g_img_email|g_img_hearts|g_img_views|g_img_main 
|------ 
|15918|11|2014/11/22/|13793150131416692879.jpg| | |333|1416692879|849259821|NULL|NULL|453|768|NULL|NULL|NULL| | |NULL|NULL|NULL|NULL|0|0|NULL 
|15919|11|2014/11/22/|4655589171416692816.jpg| | |333|1416692816|849259821|NULL|NULL|613|768|NULL|NULL|NULL| | |NULL|NULL|NULL|NULL|0|0|NULL 
|15920|11|2014/11/22/|463211921416692661.jpg| | |333|1416692661|849259821|NULL|NULL|495|475|NULL|NULL|NULL| | |NULL|NULL|NULL|NULL|0|0|NULL 
+0

我不知道如何從phpmyadmin做到這一點? – 2014-12-01 19:53:34

+0

好的。在表結構和一些樣本記錄 – 2014-12-01 20:58:16

+0

感謝額外的信息添加。每個網格至少有一個圖像? – mlinth 2014-12-01 21:11:28

回答

0

你能把條件放在查詢的WHERE部分嗎?

SELECT 
* 
FROM grids 
LEFT OUTER JOIN grid_images 
ON g_img_grid_id = g_id 
WHERE g_blog_id IS NOT NULL 
AND (g_img_main IS NOT NULL OR (g_img_main IS NULL AND g_img_width >= 475)) 
GROUP BY g_id 
ORDER BY g_id DESC 
0

這裏是下面的查詢嘗試:獲取所有對此有一個主映像,然後工會與記錄其中不存在主記錄的ID。這應該會給你每個網格的記錄...

-- Get the records with a master image. Assumes there is only one master per grid 
SELECT g.g_id, i.g_img_id -- or whatever columns you need 
FROM 
grids g 
INNER JOIN 
grid_images i on i.gm_img_grid_id = g.g_id 
WHERE i.g_img_main IS NOT NULL AND g.g_blog_id IS NOT NULL 

UNION 
-- now get the the other records. 

SELECT g.g_id,min(i.g_img_id) as g_img_id -- columns must match those in the first select 
FROM grids g 
INNER JOIN 
grid_images i on i.gm_img_grid_id = g.g_id 
WHERE i.g_img_main IS NULL 
AND g.g_blog_id IS NOT NULL 
AND i.g_img_width >=475 
GROUP BY g.g_id -- need to group by to get only one record per id