0

I have searched this issue high and low, and I felt certain that my query is valid. However, I continually get an error. Please help!

I have two tables. One first one is for residential properties. It has 4 columns that the second table doesn't have... I have used NULL AS '...' for each of the missing columns.

SELECT `L_ListingID`, `LA1_AgentID`, `L_Class`, `L_Class_`, `L_Address`, `L_City`, `L_State`, `L_Zip`, `L_AskingPrice`, `L_InputDate`, `L_Status`, `LM_Int1_1`, `LM_Int4_12`, `LM_Char10_12`, `LM_Char10_13`, `LM_Char10_14`, `LM_Char10_15` 
FROM `properties_residential` 
WHERE 1 AND ( LCASE(TRIM(`L_Address`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_City`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_Area`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_State`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_Zip`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_12`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_13`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_14`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_15`)) LIKE '%kokomo, in' OR CONCAT ( L_City, ', ', L_State ) LIKE '%kokomo, in%' OR CONCAT ( L_City, ' ', L_State ) LIKE '%kokomo, in%' ) 
UNION 
SELECT `L_ListingID`, `LA1_AgentID`, `L_Class`, `L_Class_`, `L_Address`, `L_City`, `L_State`, `L_Zip`, `L_AskingPrice`, `L_InputDate`, `L_Status`, NULL AS `LM_Int1_1`, NULL AS `LM_Int4_12`, NULL AS `LM_Char10_12`, NULL AS `LM_Char10_13`, NULL AS `LM_Char10_14`, NULL AS `LM_Char10_15` 
FROM `properties_commercial` 
WHERE 1 AND ( LCASE(TRIM(`L_Address`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_City`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_Area`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_State`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_Zip`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_12`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_13`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_14`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_15`)) LIKE '%kokomo, in' OR CONCAT ( L_City, ', ', L_State ) LIKE '%kokomo, in%' OR CONCAT ( L_City, ' ', L_State ) LIKE '%kokomo, in%' ) 
ORDER BY `L_InputDate` 
DESC LIMIT 6

And the error is...

1054 - Unknown column 'LM_Char10_12' in 'where clause'

Someone please, point me in the right direction. I greatly appreciate any help.

2
  • 2
    You could reduce all that to a much simpler query for demo purposes. Commented Feb 5, 2020 at 5:56
  • 2
    You can't use the alias of the column in the WHERE clause. In your case you don't need it. Commented Feb 5, 2020 at 6:03

1 Answer 1

1

In your query you are using column alias in WHERE clause which is illegal in MYSQL.

Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined. For example, the following query is illegal:

> SELECT id, COUNT(*) AS cnt FROM tbl_name   WHERE cnt > 0 GROUP BY id;

Above is taken from MYSQL documentation: MySQL docs

Correction in your query:

SELECT `L_ListingID`, `LA1_AgentID`, `L_Class`, `L_Class_`, `L_Address`, `L_City`, `L_State`, `L_Zip`, `L_AskingPrice`, `L_InputDate`, `L_Status`, `LM_Int1_1`, `LM_Int4_12`, `LM_Char10_12`, `LM_Char10_13`, `LM_Char10_14`, `LM_Char10_15` 
FROM `properties_residential` 
WHERE 1 AND ( LCASE(TRIM(`L_Address`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_City`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_Area`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_State`)) LIKE '%kokomo, in' OR LCASE(TRIM(`L_Zip`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_12`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_13`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_14`)) LIKE '%kokomo, in' OR LCASE(TRIM(`LM_Char10_15`)) LIKE '%kokomo, in' OR CONCAT ( L_City, ', ', L_State ) LIKE '%kokomo, in%' OR CONCAT ( L_City, ' ', L_State ) LIKE '%kokomo, in%' ) 
UNION 
SELECT `L_ListingID`
, `LA1_AgentID`
, `L_Class`
, `L_Class_`
, `L_Address`
, `L_City`
, `L_State`
, `L_Zip`
, `L_AskingPrice`
, `L_InputDate`
, `L_Status`
, NULL AS `LM_Int1_1`
, NULL AS `LM_Int4_12`
, NULL AS `LM_Char10_12`
, NULL AS `LM_Char10_13`
, NULL AS `LM_Char10_14`
, NULL AS `LM_Char10_15` 
FROM `properties_commercial` 
WHERE 1 AND 
( 
LCASE(TRIM(`L_Address`)) LIKE '%kokomo, in' 
OR LCASE(TRIM(`L_City`)) LIKE '%kokomo, in' 
OR LCASE(TRIM(`L_Area`)) LIKE '%kokomo, in' 
OR LCASE(TRIM(`L_State`)) LIKE '%kokomo, in' 
OR LCASE(TRIM(`L_Zip`)) LIKE '%kokomo, in' 
--OR LCASE(TRIM(`LM_Char10_12`)) LIKE '%kokomo, in' 
--OR LCASE(TRIM(`LM_Char10_13`)) LIKE '%kokomo, in' 
--OR LCASE(TRIM(`LM_Char10_14`)) LIKE '%kokomo, in' 
--OR LCASE(TRIM(`LM_Char10_15`)) LIKE '%kokomo, in' 
OR CONCAT ( L_City, ', ', L_State ) LIKE '%kokomo, in%' 
OR CONCAT ( L_City, ' ', L_State ) LIKE '%kokomo, in%' ) 
ORDER BY `L_InputDate` 
DESC LIMIT 6

In above query, The commented part is not required since the columns are itself NULL. So you can remove commented part from WHERE clause to make your query correct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.