为单个列取多个值的搜索存储过程
Stored procedure for search that takes multiple values for a single column
我正在建立一个网站,目前我正在处理它的搜索部分,所以我想做的是创建一个我可以调用的存储过程 returns我需要的值。
到目前为止我已经把它们放在一起了,但这限制了我只能 select 例如一个 model_id。我希望能够搜索 model_ids 1,2,3,4(示例)。
我正在考虑将参数添加为像“1,2,3,4”这样的字符串,然后将它们拆分并将其添加到临时 table 然后我可以做类似 -product_type_id 在 (select * 来自 p_product_type_id)
PROCEDURE `sp_advert_search_load`(
IN p_product_type_id varchar(255),
IN p_product_details_id INT,
IN p_advert_status_id INT,
IN p_advert_type_id INT,
IN p_manufacturer_id INT,
IN p_model_id INT,
IN p_mechanism_id INT,
IN p_calibre_id INT,
IN p_orientation_id INT,
IN p_chamber_type_id INT,
IN p_trigger_type_id INT,
IN p_condition_id INT,
IN p_barrel_type_id INT,
IN p_cased INT,
IN p_certificate_id INT ,
IN p_price_more_than FLOAT ,
IN p_price_less_than FLOAT)
BEGIN
SELECT * FROM all_information_without_images
WHERE (product_type_id IN (p_product_type_id) or p_product_type_id IS NULL)
and (product_details_id = p_product_details_id or p_product_details_id IS NULL)
and (advert_status_id = p_advert_status_id or p_advert_status_id IS NULL)
and (advert_type_id = p_advert_type_id or p_advert_type_id IS NULL)
and (manufacturer_id = p_manufacturer_id or p_manufacturer_id IS NULL)
and (model_id = p_model_id or p_model_id IS NULL)
and (mechanism_id = p_mechanism_id or p_mechanism_id IS NULL)
and (calibre_id = p_calibre_id or p_calibre_id IS NULL)
and (orientation_id = p_orientation_id or p_orientation_id IS NULL)
and (chamber_type_id = p_chamber_type_id or p_chamber_type_id IS NULL)
and (trigger_type_id = p_trigger_type_id or p_trigger_type_id IS NULL)
and (condition_id = p_condition_id or p_condition_id IS NULL)
and (barrel_type_id = p_barrel_type_id or p_barrel_type_id IS NULL)
and (cased = p_cased or p_cased IS NULL)
and (certificate_id = p_certificate_id or p_certificate_id IS NULL)
and (price >= p_price_more_than or p_price_more_than IS NULL)
and (price <= p_price_less_than or p_price_less_than IS NULL);
END
有没有更好的方法可以实现这个目标?我怀疑创建 temp tables 会大大减慢一切。任何指示都会非常有帮助。
谢谢,
理查德
我试过使用准备好的语句:
PROCEDURE `sp_advert_search_load`(
IN p_product_type_id varchar(255),
BEGIN
set @sql = concat( 'SELECT * FROM all_information_without_images
WHERE (product_type_id IN (',p_product_type_id,'))
');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
但我收到错误 1615 - 准备语句需要重新准备可能值得注意 all_information_without_images 是一个视图。
我正在考虑将我的视图代码添加到准备好的语句中,但它很长,我认为它超过了最大长度。
我运行没主意了。
任何想知道我是如何做到这一点的人你好,这里是下面的代码:
首先我使用了一个函数来拆分像'1,2,3,4'这样的字符串,然后插入到一个临时文件中table:
BEGIN
# Temp table variables
SET @tableName = 'tmpSplit';
SET @fieldName = 'variable';
# Dropping table
SET @sql := CONCAT('DROP TABLE IF EXISTS ', @tableName);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
# Creating table
SET @sql := CONCAT('CREATE TEMPORARY TABLE ', @tableName, ' (', @fieldName, ' VARCHAR(1000))');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
# Preparing toSplit
SET @vars := toSplit;
SET @vars := CONCAT("('", REPLACE(@vars, ",", "'),('"), "')");
# Inserting values
SET @sql := CONCAT('INSERT INTO ', @tableName, ' VALUES ', @vars);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
# Returning record set, or inserting into optional target
IF target IS NULL THEN
SET @sql := CONCAT('SELECT TRIM(`', @fieldName, '`) AS `', @fieldName, '` FROM ', @tableName);
ELSE
SET @sql := CONCAT('INSERT INTO ', target, ' SELECT TRIM(`', @fieldName, '`) FROM ', @tableName);
END IF;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
结束
然后 main store prod 创建了一个临时文件 table 然后使用该函数将 varchar 值插入临时文件 table 然后
WHERE
CASE WHEN @product_type_id = ''
THEN information_view.product_type_id = information_view.product_type_id
ELSE information_view.product_type_id IN (select * from tm_product_type_id)end;
PROCEDURE `sp_advert_search_load`(
IN p_product_type_id varchar(255)
)
BEGIN
DROP TEMPORARY TABLE IF EXISTS tm_product_type_id;
CREATE TEMPORARY TABLE tm_product_type_id (product_type_id int);
SET @product_type_id = ifnull(p_product_type_id,'');
CALL sp_split(@product_type_id,'tm_product_type_id');
SELECT * FROM ( SELECT
e.entity_id AS entity_id,
e.entity_type_id AS entity_type_id,
enty.entity_description AS entity_description,
e.title_id AS title_id,
t.title AS title,
e.first_name AS first_name,
e.last_name AS last_name,
e.street_line_1 AS street_line_1,
e.street_line_2 AS street_line_2,
e.street_line_3 AS street_line_3,
e.town AS town,
e.county AS county,
e.postcode AS postcode,
e.email AS email,
e.telephone AS telephone,
e.company_name AS company_name,
e.company_desc AS company_desc,
e.website AS website,
e.concent AS concent,
e.longi AS longitude,
e.lati AS latitude,
a.advert_id AS ad_advert_id,
a.title AS ad_title,
a.advert_id AS advert_id,
a.description AS advert_description,
a.advert_status_id AS advert_status_id,
ads.description AS ad_statid,
a.advert_type_id AS advert_type_id,
avt.description AS ad_type,
a.product_details_id AS product_details_id,
a.price AS price,
a.created AS created_date,
pd.product_description AS product_description,
pd.product_type_id AS product_type_id,
pt.description AS product_type_description,
g.gun_id AS gun_id,
g.manufacturer_id AS manufacturer_id,
mg.manufacturer AS manufacturer,
g.model_id AS model_id,
mo.model AS model,
g.mechanism_id AS mechanism_id,
mec.mechanism AS mechanism,
g.calibre_id AS calibre_id,
cal.calibre AS calibre,
g.orientation_id AS orientation_id,
org.orientation AS orientation,
g.chamber_type_id AS chamber_type_id,
cht.chamber_type AS chamber_description,
g.trigger_type_id AS trigger_type_id,
trgt.trigger_type AS trigger_description,
g.condition_id AS condition_id,
con.`condition` AS `condition`,
g.barrel_type_id AS barrel_type_id,
bart.barrel_type AS barrel_description,
g.cased_id AS cased_id,
c.cased AS cased,
g.barrel_length_inches_id AS barrel_length_inches_id,
bli.barrel_length_inches AS barrel_length_inches,
g.barrel_length_inches_fraction_id AS barrel_length_inches_fraction_id,
blif.barrel_length_inches_fraction AS barrel_length_inches_fraction,
g.stock_length_inches_id AS stock_length_inches_id,
sli.stock_length_inches AS stock_length_inches,
g.stock_length_inches_fraction_id AS stock_length_inches_fraction_id,
slif.stock_length_inches_fraction AS stock_length_inches_fraction,
wp.weight_pound_id AS weight_pound_id,
wp.weight_pound AS weight_pound,
wo.weight_ounce_id AS weight_ounce_id,
wo.weight_ounce AS weight_ounce,
g.choke_type_id AS choke_type_id,
chot.choke_type AS choke_type,
g.choke_type_2_id AS choke_type_2_id,
chot2.choke_type AS choke_type_2,
g.ejection_id AS ejection_id,
ejec.ejection AS ejection,
g.certificate_id AS certificate_id,
cert.certificate AS certificate,
g.serial_number AS serial_number,
g.stock_number AS stock_number,
a.url_data AS url_data,
op.optic_id AS optic_id,
op.product_details_id AS optic_product_details_id,
pdop.product_description AS optic_product_description,
op.optic_make_id AS optic_make_id,
om.optic_make AS optic_make,
op.optic_custom_make AS optic_custom_make,
op.optic_model_id AS optic_model_id,
omod.optic_model AS optic_model,
op.optic_custom_model AS optic_custom_model,
op.optic_magnification_id AS optic_magnification_id,
omag.optic_magnification AS optic_magnification,
op.optic_custom_magnification AS optic_custom_magnification,
op.condition_id AS optic_condition_id,
optcon.condition AS optic_condition,
equ.equipment_id AS equipment_id,
equ.equipment_type_id AS equipment_type_id,
equt.equipment_type AS equipment_type,
equ.equipment_make_id AS equipment_make_id,
equm.equipment_make AS equipment_make,
equ.equipment_custom_model AS equipment_custom_model,
equ.condition_id AS equipment_condition_id,
equc.condition AS equipment_condition,
opa.optic_make_id AS optic_advert_optic_make_id,
oma.optic_make AS optic_advert_optic_make,
opa.optic_custom_make AS optic_advert_custom_make,
opa.optic_model_id AS optic_advert_model_id,
omoda.optic_model AS optic_advert_model,
opa.optic_custom_model AS optic_advert_custom_model,
opa.optic_magnification_id AS optic_advert_magnification_id,
omaga.optic_magnification AS optic_advert_magnification,
opa.optic_custom_magnification AS optic_advert_custom_magnification,
opa.condition_id AS optic_advert_condition_id,
optcona.condition AS optic_advert_condition,
img.image_id AS image_id,
img.image_src AS image_src
FROM
tbl_entity e
JOIN tbl_adverts a ON e.entity_id = a.entity_id
LEFT JOIN tbl_entity_type enty ON enty.entity_type_id = e.entity_type_id
LEFT JOIN tbl_title t ON t.title_id = e.title_id
LEFT JOIN tbl_guns g ON a.gun_id = g.gun_id
LEFT JOIN tbl_equipments equ ON a.equipment_id = equ.equipment_id
LEFT JOIN tbl_equipment_type equt ON equt.equipment_type_id = equ.equipment_type_id
LEFT JOIN tbl_equipment_make equm ON equm.equipment_make_id = equ.equipment_make_id
LEFT JOIN tbl_condition equc ON equc.condition_id = equ.condition_id
LEFT JOIN tbl_product_details pd ON a.product_details_id = pd.product_details_id AND a.product_type_id = pd.product_type_id
LEFT JOIN tbl_advert_type avt ON avt.advert_type_id = a.advert_type_id
LEFT JOIN tbl_advert_status ads ON ads.advert_status_id = a.advert_status_id
LEFT JOIN tbl_manufacturer mg ON g.manufacturer_id = mg.manufacturer_id AND mg.product_details_id = a.product_details_id
LEFT JOIN tbl_model mo ON g.product_details_id = mo.product_details_id AND mo.model_id = g.model_id
LEFT JOIN tbl_mechanism mec ON mec.product_details_id = g.product_details_id AND mec.mechanism_id = g.mechanism_id
LEFT JOIN tbl_calibre cal ON cal.product_details_id = g.product_details_id AND cal.calibre_id = g.calibre_id
LEFT JOIN tbl_barrel_type bart ON bart.product_details_id = g.product_details_id AND bart.barrel_type_id = g.barrel_type_id
LEFT JOIN tbl_chamber_type cht ON cht.product_details_id = g.product_details_id AND g.chamber_type_id = cht.chamber_type_id
LEFT JOIN tbl_trigger_type trgt ON trgt.product_details_id = g.product_details_id AND g.trigger_type_id = trgt.trigger_type_id
LEFT JOIN tbl_orientation org ON g.orientation_id = org.orientation_id
LEFT JOIN tbl_condition con ON g.condition_id = con.condition_id
LEFT JOIN tbl_cased c ON g.cased_id = c.cased_id
LEFT JOIN tbl_certificate cert ON cert.product_details_id = g.product_details_id AND cert.certificate_id = g.certificate_id
LEFT JOIN tbl_product_type pt ON a.product_type_id = pt.product_type_id
LEFT JOIN tbl_images img ON a.advert_id = img.advert_id AND img.primary_flag = 'Y'
LEFT JOIN tbl_stock_length_inches sli ON a.product_details_id = sli.product_details_id AND sli.stock_length_inches_id = g.stock_length_inches_id
LEFT JOIN tbl_stock_length_inches_fraction slif ON slif.stock_length_inches_fraction_id = g.stock_length_inches_fraction_id
LEFT JOIN tbl_barrel_length_inches bli ON a.product_details_id = bli.product_details_id AND bli.barrel_length_inches_id = g.barrel_length_inches_id
LEFT JOIN tbl_barrel_length_inches_fraction blif ON blif.barrel_length_inches_fraction_id = g.barrel_length_inches_fraction_id
LEFT JOIN tbl_choke_type chot ON a.product_details_id = chot.product_details_id AND chot.choke_type_id = g.choke_type_id
LEFT JOIN tbl_choke_type chot2 ON a.product_details_id = chot.product_details_id AND chot2.choke_type_id = g.choke_type_2_id
LEFT JOIN tbl_ejection ejec ON g.ejection_id = ejec.ejection_id
LEFT JOIN tbl_weight_pounds wp ON wp.weight_pound_id = g.weight_pound_id
LEFT JOIN tbl_weight_ounces wo ON wo.weight_ounce_id = g.weight_ounce_id
LEFT JOIN tbl_optics op ON op.optic_id = a.optic_id AND a.gun_id IS NOT NULL
LEFT JOIN tbl_product_details pdop ON pdop.product_details_id = op.product_details_id
LEFT JOIN tbl_optic_make om ON op.optic_make_id = om.optic_make_id AND op.product_details_id = om.product_details_id
LEFT JOIN tbl_optic_model omod ON op.optic_model_id = omod.optic_model_id AND omod.optic_make_id = om.optic_make_id
LEFT JOIN tbl_optic_magnification omag ON omag.optic_magnification_id = op.optic_magnification_id
LEFT JOIN tbl_condition optcon ON op.condition_id = optcon.condition_id
LEFT JOIN tbl_optics opa ON opa.optic_id = a.optic_id AND a.gun_id is null
LEFT JOIN tbl_product_details pdopa ON pdopa.product_details_id = opa.product_details_id
LEFT JOIN tbl_optic_make oma ON opa.optic_make_id = oma.optic_make_id AND opa.product_details_id = oma.product_details_id
LEFT JOIN tbl_optic_model omoda ON opa.optic_model_id = omoda.optic_model_id AND omoda.optic_make_id = oma.optic_make_id
LEFT JOIN tbl_optic_magnification omaga ON omaga.optic_magnification_id = opa.optic_magnification_id
LEFT JOIN tbl_condition optcona ON opa.condition_id = optcona.condition_id
ORDER BY
e.entity_id,
a.advert_id) information_view
WHERE
CASE WHEN @product_type_id = ''
THEN information_view.product_type_id = information_view.product_type_id
ELSE information_view.product_type_id IN (select * from tm_product_type_id)end;
END
我正在建立一个网站,目前我正在处理它的搜索部分,所以我想做的是创建一个我可以调用的存储过程 returns我需要的值。
到目前为止我已经把它们放在一起了,但这限制了我只能 select 例如一个 model_id。我希望能够搜索 model_ids 1,2,3,4(示例)。
我正在考虑将参数添加为像“1,2,3,4”这样的字符串,然后将它们拆分并将其添加到临时 table 然后我可以做类似 -product_type_id 在 (select * 来自 p_product_type_id)
PROCEDURE `sp_advert_search_load`(
IN p_product_type_id varchar(255),
IN p_product_details_id INT,
IN p_advert_status_id INT,
IN p_advert_type_id INT,
IN p_manufacturer_id INT,
IN p_model_id INT,
IN p_mechanism_id INT,
IN p_calibre_id INT,
IN p_orientation_id INT,
IN p_chamber_type_id INT,
IN p_trigger_type_id INT,
IN p_condition_id INT,
IN p_barrel_type_id INT,
IN p_cased INT,
IN p_certificate_id INT ,
IN p_price_more_than FLOAT ,
IN p_price_less_than FLOAT)
BEGIN
SELECT * FROM all_information_without_images
WHERE (product_type_id IN (p_product_type_id) or p_product_type_id IS NULL)
and (product_details_id = p_product_details_id or p_product_details_id IS NULL)
and (advert_status_id = p_advert_status_id or p_advert_status_id IS NULL)
and (advert_type_id = p_advert_type_id or p_advert_type_id IS NULL)
and (manufacturer_id = p_manufacturer_id or p_manufacturer_id IS NULL)
and (model_id = p_model_id or p_model_id IS NULL)
and (mechanism_id = p_mechanism_id or p_mechanism_id IS NULL)
and (calibre_id = p_calibre_id or p_calibre_id IS NULL)
and (orientation_id = p_orientation_id or p_orientation_id IS NULL)
and (chamber_type_id = p_chamber_type_id or p_chamber_type_id IS NULL)
and (trigger_type_id = p_trigger_type_id or p_trigger_type_id IS NULL)
and (condition_id = p_condition_id or p_condition_id IS NULL)
and (barrel_type_id = p_barrel_type_id or p_barrel_type_id IS NULL)
and (cased = p_cased or p_cased IS NULL)
and (certificate_id = p_certificate_id or p_certificate_id IS NULL)
and (price >= p_price_more_than or p_price_more_than IS NULL)
and (price <= p_price_less_than or p_price_less_than IS NULL);
END
有没有更好的方法可以实现这个目标?我怀疑创建 temp tables 会大大减慢一切。任何指示都会非常有帮助。
谢谢,
理查德
我试过使用准备好的语句:
PROCEDURE `sp_advert_search_load`(
IN p_product_type_id varchar(255),
BEGIN
set @sql = concat( 'SELECT * FROM all_information_without_images
WHERE (product_type_id IN (',p_product_type_id,'))
');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
但我收到错误 1615 - 准备语句需要重新准备可能值得注意 all_information_without_images 是一个视图。
我正在考虑将我的视图代码添加到准备好的语句中,但它很长,我认为它超过了最大长度。
我运行没主意了。
任何想知道我是如何做到这一点的人你好,这里是下面的代码:
首先我使用了一个函数来拆分像'1,2,3,4'这样的字符串,然后插入到一个临时文件中table:
BEGIN
# Temp table variables
SET @tableName = 'tmpSplit';
SET @fieldName = 'variable';
# Dropping table
SET @sql := CONCAT('DROP TABLE IF EXISTS ', @tableName);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
# Creating table
SET @sql := CONCAT('CREATE TEMPORARY TABLE ', @tableName, ' (', @fieldName, ' VARCHAR(1000))');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
# Preparing toSplit
SET @vars := toSplit;
SET @vars := CONCAT("('", REPLACE(@vars, ",", "'),('"), "')");
# Inserting values
SET @sql := CONCAT('INSERT INTO ', @tableName, ' VALUES ', @vars);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
# Returning record set, or inserting into optional target
IF target IS NULL THEN
SET @sql := CONCAT('SELECT TRIM(`', @fieldName, '`) AS `', @fieldName, '` FROM ', @tableName);
ELSE
SET @sql := CONCAT('INSERT INTO ', target, ' SELECT TRIM(`', @fieldName, '`) FROM ', @tableName);
END IF;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
结束
然后 main store prod 创建了一个临时文件 table 然后使用该函数将 varchar 值插入临时文件 table 然后
WHERE
CASE WHEN @product_type_id = ''
THEN information_view.product_type_id = information_view.product_type_id
ELSE information_view.product_type_id IN (select * from tm_product_type_id)end;
PROCEDURE `sp_advert_search_load`(
IN p_product_type_id varchar(255)
)
BEGIN
DROP TEMPORARY TABLE IF EXISTS tm_product_type_id;
CREATE TEMPORARY TABLE tm_product_type_id (product_type_id int);
SET @product_type_id = ifnull(p_product_type_id,'');
CALL sp_split(@product_type_id,'tm_product_type_id');
SELECT * FROM ( SELECT
e.entity_id AS entity_id,
e.entity_type_id AS entity_type_id,
enty.entity_description AS entity_description,
e.title_id AS title_id,
t.title AS title,
e.first_name AS first_name,
e.last_name AS last_name,
e.street_line_1 AS street_line_1,
e.street_line_2 AS street_line_2,
e.street_line_3 AS street_line_3,
e.town AS town,
e.county AS county,
e.postcode AS postcode,
e.email AS email,
e.telephone AS telephone,
e.company_name AS company_name,
e.company_desc AS company_desc,
e.website AS website,
e.concent AS concent,
e.longi AS longitude,
e.lati AS latitude,
a.advert_id AS ad_advert_id,
a.title AS ad_title,
a.advert_id AS advert_id,
a.description AS advert_description,
a.advert_status_id AS advert_status_id,
ads.description AS ad_statid,
a.advert_type_id AS advert_type_id,
avt.description AS ad_type,
a.product_details_id AS product_details_id,
a.price AS price,
a.created AS created_date,
pd.product_description AS product_description,
pd.product_type_id AS product_type_id,
pt.description AS product_type_description,
g.gun_id AS gun_id,
g.manufacturer_id AS manufacturer_id,
mg.manufacturer AS manufacturer,
g.model_id AS model_id,
mo.model AS model,
g.mechanism_id AS mechanism_id,
mec.mechanism AS mechanism,
g.calibre_id AS calibre_id,
cal.calibre AS calibre,
g.orientation_id AS orientation_id,
org.orientation AS orientation,
g.chamber_type_id AS chamber_type_id,
cht.chamber_type AS chamber_description,
g.trigger_type_id AS trigger_type_id,
trgt.trigger_type AS trigger_description,
g.condition_id AS condition_id,
con.`condition` AS `condition`,
g.barrel_type_id AS barrel_type_id,
bart.barrel_type AS barrel_description,
g.cased_id AS cased_id,
c.cased AS cased,
g.barrel_length_inches_id AS barrel_length_inches_id,
bli.barrel_length_inches AS barrel_length_inches,
g.barrel_length_inches_fraction_id AS barrel_length_inches_fraction_id,
blif.barrel_length_inches_fraction AS barrel_length_inches_fraction,
g.stock_length_inches_id AS stock_length_inches_id,
sli.stock_length_inches AS stock_length_inches,
g.stock_length_inches_fraction_id AS stock_length_inches_fraction_id,
slif.stock_length_inches_fraction AS stock_length_inches_fraction,
wp.weight_pound_id AS weight_pound_id,
wp.weight_pound AS weight_pound,
wo.weight_ounce_id AS weight_ounce_id,
wo.weight_ounce AS weight_ounce,
g.choke_type_id AS choke_type_id,
chot.choke_type AS choke_type,
g.choke_type_2_id AS choke_type_2_id,
chot2.choke_type AS choke_type_2,
g.ejection_id AS ejection_id,
ejec.ejection AS ejection,
g.certificate_id AS certificate_id,
cert.certificate AS certificate,
g.serial_number AS serial_number,
g.stock_number AS stock_number,
a.url_data AS url_data,
op.optic_id AS optic_id,
op.product_details_id AS optic_product_details_id,
pdop.product_description AS optic_product_description,
op.optic_make_id AS optic_make_id,
om.optic_make AS optic_make,
op.optic_custom_make AS optic_custom_make,
op.optic_model_id AS optic_model_id,
omod.optic_model AS optic_model,
op.optic_custom_model AS optic_custom_model,
op.optic_magnification_id AS optic_magnification_id,
omag.optic_magnification AS optic_magnification,
op.optic_custom_magnification AS optic_custom_magnification,
op.condition_id AS optic_condition_id,
optcon.condition AS optic_condition,
equ.equipment_id AS equipment_id,
equ.equipment_type_id AS equipment_type_id,
equt.equipment_type AS equipment_type,
equ.equipment_make_id AS equipment_make_id,
equm.equipment_make AS equipment_make,
equ.equipment_custom_model AS equipment_custom_model,
equ.condition_id AS equipment_condition_id,
equc.condition AS equipment_condition,
opa.optic_make_id AS optic_advert_optic_make_id,
oma.optic_make AS optic_advert_optic_make,
opa.optic_custom_make AS optic_advert_custom_make,
opa.optic_model_id AS optic_advert_model_id,
omoda.optic_model AS optic_advert_model,
opa.optic_custom_model AS optic_advert_custom_model,
opa.optic_magnification_id AS optic_advert_magnification_id,
omaga.optic_magnification AS optic_advert_magnification,
opa.optic_custom_magnification AS optic_advert_custom_magnification,
opa.condition_id AS optic_advert_condition_id,
optcona.condition AS optic_advert_condition,
img.image_id AS image_id,
img.image_src AS image_src
FROM
tbl_entity e
JOIN tbl_adverts a ON e.entity_id = a.entity_id
LEFT JOIN tbl_entity_type enty ON enty.entity_type_id = e.entity_type_id
LEFT JOIN tbl_title t ON t.title_id = e.title_id
LEFT JOIN tbl_guns g ON a.gun_id = g.gun_id
LEFT JOIN tbl_equipments equ ON a.equipment_id = equ.equipment_id
LEFT JOIN tbl_equipment_type equt ON equt.equipment_type_id = equ.equipment_type_id
LEFT JOIN tbl_equipment_make equm ON equm.equipment_make_id = equ.equipment_make_id
LEFT JOIN tbl_condition equc ON equc.condition_id = equ.condition_id
LEFT JOIN tbl_product_details pd ON a.product_details_id = pd.product_details_id AND a.product_type_id = pd.product_type_id
LEFT JOIN tbl_advert_type avt ON avt.advert_type_id = a.advert_type_id
LEFT JOIN tbl_advert_status ads ON ads.advert_status_id = a.advert_status_id
LEFT JOIN tbl_manufacturer mg ON g.manufacturer_id = mg.manufacturer_id AND mg.product_details_id = a.product_details_id
LEFT JOIN tbl_model mo ON g.product_details_id = mo.product_details_id AND mo.model_id = g.model_id
LEFT JOIN tbl_mechanism mec ON mec.product_details_id = g.product_details_id AND mec.mechanism_id = g.mechanism_id
LEFT JOIN tbl_calibre cal ON cal.product_details_id = g.product_details_id AND cal.calibre_id = g.calibre_id
LEFT JOIN tbl_barrel_type bart ON bart.product_details_id = g.product_details_id AND bart.barrel_type_id = g.barrel_type_id
LEFT JOIN tbl_chamber_type cht ON cht.product_details_id = g.product_details_id AND g.chamber_type_id = cht.chamber_type_id
LEFT JOIN tbl_trigger_type trgt ON trgt.product_details_id = g.product_details_id AND g.trigger_type_id = trgt.trigger_type_id
LEFT JOIN tbl_orientation org ON g.orientation_id = org.orientation_id
LEFT JOIN tbl_condition con ON g.condition_id = con.condition_id
LEFT JOIN tbl_cased c ON g.cased_id = c.cased_id
LEFT JOIN tbl_certificate cert ON cert.product_details_id = g.product_details_id AND cert.certificate_id = g.certificate_id
LEFT JOIN tbl_product_type pt ON a.product_type_id = pt.product_type_id
LEFT JOIN tbl_images img ON a.advert_id = img.advert_id AND img.primary_flag = 'Y'
LEFT JOIN tbl_stock_length_inches sli ON a.product_details_id = sli.product_details_id AND sli.stock_length_inches_id = g.stock_length_inches_id
LEFT JOIN tbl_stock_length_inches_fraction slif ON slif.stock_length_inches_fraction_id = g.stock_length_inches_fraction_id
LEFT JOIN tbl_barrel_length_inches bli ON a.product_details_id = bli.product_details_id AND bli.barrel_length_inches_id = g.barrel_length_inches_id
LEFT JOIN tbl_barrel_length_inches_fraction blif ON blif.barrel_length_inches_fraction_id = g.barrel_length_inches_fraction_id
LEFT JOIN tbl_choke_type chot ON a.product_details_id = chot.product_details_id AND chot.choke_type_id = g.choke_type_id
LEFT JOIN tbl_choke_type chot2 ON a.product_details_id = chot.product_details_id AND chot2.choke_type_id = g.choke_type_2_id
LEFT JOIN tbl_ejection ejec ON g.ejection_id = ejec.ejection_id
LEFT JOIN tbl_weight_pounds wp ON wp.weight_pound_id = g.weight_pound_id
LEFT JOIN tbl_weight_ounces wo ON wo.weight_ounce_id = g.weight_ounce_id
LEFT JOIN tbl_optics op ON op.optic_id = a.optic_id AND a.gun_id IS NOT NULL
LEFT JOIN tbl_product_details pdop ON pdop.product_details_id = op.product_details_id
LEFT JOIN tbl_optic_make om ON op.optic_make_id = om.optic_make_id AND op.product_details_id = om.product_details_id
LEFT JOIN tbl_optic_model omod ON op.optic_model_id = omod.optic_model_id AND omod.optic_make_id = om.optic_make_id
LEFT JOIN tbl_optic_magnification omag ON omag.optic_magnification_id = op.optic_magnification_id
LEFT JOIN tbl_condition optcon ON op.condition_id = optcon.condition_id
LEFT JOIN tbl_optics opa ON opa.optic_id = a.optic_id AND a.gun_id is null
LEFT JOIN tbl_product_details pdopa ON pdopa.product_details_id = opa.product_details_id
LEFT JOIN tbl_optic_make oma ON opa.optic_make_id = oma.optic_make_id AND opa.product_details_id = oma.product_details_id
LEFT JOIN tbl_optic_model omoda ON opa.optic_model_id = omoda.optic_model_id AND omoda.optic_make_id = oma.optic_make_id
LEFT JOIN tbl_optic_magnification omaga ON omaga.optic_magnification_id = opa.optic_magnification_id
LEFT JOIN tbl_condition optcona ON opa.condition_id = optcona.condition_id
ORDER BY
e.entity_id,
a.advert_id) information_view
WHERE
CASE WHEN @product_type_id = ''
THEN information_view.product_type_id = information_view.product_type_id
ELSE information_view.product_type_id IN (select * from tm_product_type_id)end;
END