简体   繁体   中英

Unknown Column In Where Clause (as)

SET @value = '';
SELECT 
    id, order_kind, property_kind, town, images, agreement, uprice, eprice,
    tprice, pprice, bprice, qprice, status, createtime, town_title, fullname, 
    mobile, profile_image, area, barea, address, 
    CONCAT(id+9000) AS sid,
    CONCAT(area,' m') AS marea,
    CONCAT(barea,' m') AS mbarea 
FROM `sells` 
WHERE `order_kind` = '$order_kind' 
  AND `property_kind` = '$property_kind' 
  AND (
      sid LIKE CONCAT('%', @value, '%') || 
      town_title LIKE CONCAT('%', @value, '%') || 
      fullname LIKE CONCAT('%', @value, '%') || 
      mobile LIKE CONCAT('%', @value, '%') || 
      marea LIKE CONCAT('%', @value, '%') || 
      mbarea LIKE CONCAT('%', @value, '%') || 
      address LIKE CONCAT('%', @value, '%') 
  )

I use this sql for searching, and erroring unknown column 'sid' in where clause
i change where to having but not work php loop

Your query defines sid as CONCAT(id+9000) AS sid in the SELECT clause. You can't reuse that alias in the WHERE clause. Instead, you need to repeat the expression.

So basically change this condition:

sid LIKE CONCAT('%', @value, '%')

To:

CONCAT(id+9000) LIKE CONCAT('%', @value, '%')

Important note: use prepared statements: Do not concatenate PHP variables in the query string. this is both inefficient and unsafe: Recommended reading: How can I prevent SQL injection in PHP?

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