{"id":15944052,"date":"2022-08-24T02:38:49","date_gmt":"2022-08-24T02:38:49","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/sql-query-conditional\/"},"modified":"2022-08-24T02:38:49","modified_gmt":"2022-08-24T02:38:49","slug":"sql-query-conditional","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/sql-query-conditional\/","title":{"rendered":"SQL Query Conditional"},"content":{"rendered":"<p>Greetings. I used pods to create the custom lost type &#8220;Lot&#8221;. Each lot has a field called &#8220;lot_status&#8221;. I am trying to write an SQL query to show all lots with a specific lot status. When I run it with no conditions, I get all the results. However, when I try to add a conditional WHERE clause, I get an &#8216;unknown column&#8217; error. I have tried lots of different combinations of meta_value and lot_status, but I keep getting the unknown column error.<\/p>\n<pre><code>SELECT posts_lot.post_title AS lot_post_title,\n       posts_lot.ID AS lot_ID,\n       posts_lot.post_content AS lot_post_content,\n       lot_meta_lot_status_tbl.meta_value AS lot_meta_lot_status,\n       lot_meta_room_tbl.meta_value AS lot_meta_room,\n       lot_meta_product_tbl.meta_value AS lot_meta_product\nFROM fzMCGsSFposts AS posts_lot\n  INNER JOIN (SELECT lot_meta_lot_status_tbl_posts.ID as id, meta_value, meta_key  FROM fzMCGsSFpostmeta AS lot_meta_lot_status_tbl_postmeta  INNER JOIN fzMCGsSFposts AS lot_meta_lot_status_tbl_posts   ON lot_meta_lot_status_tbl_postmeta.post_id = lot_meta_lot_status_tbl_posts.ID   AND lot_meta_lot_status_tbl_posts.post_type = &#039;lot&#039;) AS lot_meta_lot_status_tbl\n     ON lot_meta_lot_status_tbl.meta_key = &#039;lot_status&#039; AND lot_meta_lot_status_tbl.id = posts_lot.ID \n  INNER JOIN (SELECT lot_meta_room_tbl_posts.ID as id, meta_value, meta_key  FROM fzMCGsSFpostmeta AS lot_meta_room_tbl_postmeta  INNER JOIN fzMCGsSFposts AS lot_meta_room_tbl_posts   ON lot_meta_room_tbl_postmeta.post_id = lot_meta_room_tbl_posts.ID   AND lot_meta_room_tbl_posts.post_type = &#039;lot&#039;) AS lot_meta_room_tbl\n     ON lot_meta_room_tbl.meta_key = &#039;room&#039; AND lot_meta_room_tbl.id = posts_lot.ID \n  INNER JOIN (SELECT lot_meta_product_tbl_posts.ID as id, meta_value, meta_key  FROM fzMCGsSFpostmeta AS lot_meta_product_tbl_postmeta  INNER JOIN fzMCGsSFposts AS lot_meta_product_tbl_posts   ON lot_meta_product_tbl_postmeta.post_id = lot_meta_product_tbl_posts.ID   AND lot_meta_product_tbl_posts.post_type = &#039;lot&#039;) AS lot_meta_product_tbl\n     ON lot_meta_product_tbl.meta_key = &#039;product&#039; AND lot_meta_product_tbl.id = posts_lot.ID \nWHERE 1=1 \n   AND posts_lot.post_content &gt; &#039;0&#039;\n   AND posts_lot.post_type = &#039;lot&#039;\n   AND lot_status.meta_value = &#039;1199&#039;<\/code><\/pre>\n<p>Can you help me find the proper syntax to target the lot_status field with a where function?<\/p>\n<p>Thanks very much! Let me know if you need more information.<\/p>\n","protected":false},"template":"","class_list":["post-15944052","topic","type-topic","status-publish","hentry","topic-tag-meta_value","topic-tag-sql","topic-tag-sql-query","topic-tag-unknown-column","topic-tag-where"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/15944052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/15944052\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=15944052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}