简体   繁体   中英

Pull result for missing data

How to match and product_id in the query atm the query is pull only missing result but each product have his own record and need to match with this record

This query work right but isn't match product_id

SELECT oc_ekstri_frontend.*,
      oc_ekstri_image.id as img_id,
      oc_ekstri_image.name
      FROM oc_ekstri_image
LEFT JOIN oc_ekstri_frontend ON (oc_ekstri_frontend.id = oc_ekstri_image.frontend_id)
LEFT JOIN oc_ekstri_frontend_view ON (oc_ekstri_frontend_view.ekstri_id = oc_ekstri_image.id)
      WHERE oc_ekstri_frontend.subcat_id = '" . $id . "'
     AND oc_ekstri_frontend_view.ekstri_id IS NULL

How will I match for current product_id? I tried something like this to add to the query but isn't work result is empty

AND oc_ekstri_frontend_view.product_id = '" . $product_id . "'

Without seeing your data or your expected output, it is hard to give an exact answer. One possible solution might be to move your new restriction from the WHERE clause to the ON clause:

SELECT
    f.*,
    i.id AS img_id,
    i.name
FROM oc_ekstri_image i
LEFT JOIN oc_ekstri_frontend f
    ON f.id = i.frontend_id
LEFT JOIN oc_ekstri_frontend_view v
    ON v.ekstri_id = i.id AND
       v.product_id = ?
WHERE
    f.subcat_id = ? AND
    v.ekstri_id IS NULL;

I use ? placeholders in the above query to suggest that you use a prepared statement rather than concatenating in your parameters. If you don't know what prepared statements are in PHP, you should read about them .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM