I'm working with a SQL query and I need to display a set of information, but I have 5 records to display. The following query isn't working:
SELECT m.customer_number, m.last_name, m.billing_address_line_1, m.billing_address_line_2, m.billing_city, m.billing_state, m.phone_number1, c.equipment_serial_no, c.make, c.model_number, c.color_code
FROM customer_master as m
INNER JOIN equip_master as c ON m.customer_number = c.customer_number
WHERE m.customer_number = '19975107' AND '1039283' AND '39203821' AND '23824917' AND '1833729'
I get the following error:
Msg 4145, Level 15, State 1, Line 9 An expression of non-boolean type
specified in a context where a condition is expected, near '19978107'
That's not a valid syntax. If you want to get the list of customer_number then use IN clause.
....
WHERE m.customer_number in('19975107', '1039283', '39203821'
,'23824917' , '1833729')
You Have syntax error in your query.
It must be written like this
SELECT m.customer_number, m.last_name, m.billing_address_line_1,m.billing_address_line_2, m.billing_city, m.billing_state, m.phone_number1, c.equipment_serial_no, c.make, c.model_number, c.color_code
FROM customer_master as m
INNER JOIN equip_master as c
ON m.customer_number = c.customer_number
WHERE m.customer_number = '19975107' OR m.customer_number='1039283' OR m.customer_number='39203821' OR m.customer_number='23824917' OR m.customer_number ='1833729';
Note: Since customer_number is a primary key hence no two customer can have same value of it. Hence using AND is logically incorrect it will return empty set.
Related
I am trying to perform an inner join on two tables by date and county ID. Whenever I run the query, I get the following error:
SQL Compilation Error: syntax error line 1 at position 0 unexpected 'INNER'
The code is as follows:
SELECT
ISO3166_1,
FIPS,
DATE,
CASES
FROM
"covid-19","public"."timeseries"
WHERE
ISO3166_1 = 'US'
INNER JOIN
"db"."population_schema"."pop_table"
ON
(("covid-19","public"."timeseries".fips = "db"."population_schema"."pop_table".fips)
AND
("covid-19","public"."timeseries".date= "db"."population_schema"."pop_table".date))
For reference, the date and FIPS data types are the same. Are there any glaringly obvious issues?
The WHERE clauses go after all JOINS:
SELECT
ISO3166_1,
FIPS,
DATE,
CASES
FROM "covid-19","public"."timeseries" AS a
INNER JOIN "db"."population_schema"."pop_table" AS b
ON a.fips = b.fips
AND a.date= b.date
WHERE
ISO3166_1 = 'US'
and if you alias those two tables, your join logic can be much more readable.
I want to get student details from different tables with given academic year and class.
Query that i used is:
SELECT
list_acad_years.acad_year,
a.fk_stu_id,
tbl_stu_details.stu_fname,
tbl_stu_details.stu_sname,
a.fk_section_id,
b.fk_class_id,
list_class.class_name,
list_sections.section_code
FROM
tbl_stu_details,
list_class,
list_sections,
list_acad_years
INNER JOIN
tbl_stu_class AS a
ON
(
list_acad_years.pk_acad_year_id = a.fk_year_id
)
INNER JOIN
tbl_stu_class AS b
ON
(list_class.pk_class_id = b.fk_class_id)
WHERE
(
list_acad_years.acad_year = '2019'
) AND(list_class.class_name = '10')
it shows the following error:
#1054 - Unknown column 'list_class.pk_class_id' in 'on clause'
columns of my table are:
tbl_stu_class:
pk_stu_cls_id`, `fk_stu_id`, `fk_year_id`, `fk_class_id`, `fk_section_id`, `current_yr`
list_class:
`pk_class_id`, `class_name`, `class_code`, `fk_user_id`
list_sections:
pk_section_id`, `section_code`, `section_description`, `fk_user_id`
list_acad_years:
`pk_acad_year_id`, `acad_year`, `acad_year_code`, `fk_user_id`
tbl_stu_details:
`pk_stu_id`, `stu_id`, `username`, `stu_fname`, `stu_mname`, `stu_sname`
list_sections:
`pk_section_id`, `section_code`, `section_description`, `fk_user_id`
Why did it say unknown column when the column is present?
It would be great help if you can help me make this query better...
Thanks in advance.
Your problem is that you are using old-style joins. Period. To make matters worse, you are combining them with new style joins.
The names of the tables are not understood across commas. That limits the scope of the definitions.
You appear to know how to write JOINs correctly. So, just fix the FROM clause and your code should work.
Your Inner join will alway connect to the last table in from statement if you are using multiple FROM table.
I have just change the position of INNER JOIN
try this let me know if it works.
SELECT
list_acad_years.acad_year,
a.fk_stu_id,
tbl_stu_details.stu_fname,
tbl_stu_details.stu_sname,
a.fk_section_id,
b.fk_class_id,
list_class.class_name,
list_sections.section_code
FROM
tbl_stu_details,
list_class INNER JOIN
tbl_stu_class AS b
ON
(list_class.pk_class_id = b.fk_class_id),
list_sections,
list_acad_years
INNER JOIN
tbl_stu_class AS a
ON
(
list_acad_years.pk_acad_year_id = a.fk_year_id
)
WHERE
(
list_acad_years.acad_year = '2019'
) AND(list_class.class_name = '10')
SELECT
c.CONSOL_INVOICE,
cu.name,
cu.CUST_CODE,
c.bu_name,
cLang.name
FROM CONSL_INV c
LEFT JOIN customers cu ON c.cust_code = cu.CUST_CODE,
customers_lang cLang
WHERE
upper(cLang.NAME) LIKE ?
AND upper(cLang.LANGUAGE_CD) = ?
AND c.CUST_CODE = cLang.CUST_CODE
This Query Executes Correctly In Oracle Sql Developer but it does not execute in hibernate.
It gives the following error:
"ORA-00918: column ambiguously defined"
I know it's because of multiple columns having same name, but I have done it correctly but still don't know why it's not executing in hibernate.
As the others suggested use aliases:
select c.consol_invoice
,cu.name as name1
,cu.cust_code
,c.bu_name
,clang.name as name2
from consl_inv c
left join customers cu
on c.cust_code = cu.cust_code, customers_lang clang
where upper(clang.name) like ?
and upper(clang.language_cd) = ?
and c.cust_code = clang.cust_code
As you can see I've added aliases to the selected columns cu.name and clang.name so that the query result has two different columns.
I am getting ORA-00904 "Invalid Identifier" when I use a legitimate column name in a left join, but I do not get an error using the same identifier (I copied and pasted to be sure) in the where clause if I use a standard (inner) join on the table I was trying to limit.
Example 1:
Select NAMES.R_NAME
From CMPLN, NAMES
Where CMPLN.CMPLN_ID = NAMES.CMPLN_ID
Works.
EXAMPLE 2:
Select NAMES.R_NAME
From CMPLN
Left Join NAMES On CMPLN.CMPLN_ID = NAMES.CMPLN_ID
Gets an error.
"ORA-00904: "CMPLN"."CMPLN_ID": invalid identifier
I am using Oracle.
(From an answer posted by the asker Jul 29 '15 at 12:40 that should have been an edit:)
I didn't present the code correctly for the answer to be apparent.
I had inserted another table in the From clause between CMPLN and the join, like this:
Select NAMES.R_NAME
From CMPLN, RSPN
Left Join NAMES On CMPLN.CMPLN_ID = NAMES.CMPLN_ID
That is what caused the error.
(In the Informix SQL I am used to this would have worked, but not in Oracle.)
I had a similar issue: ORA-00904: "TTR"."PARENT_CNTL_NBR": invalid identifier as per the below:
SELECT *
FROM TTR,
TST
LEFT JOIN ITF
ON NVL(TRIM(TST.CLIENT_USE_TXT), TRIM(SUBSTR(TTR.PARENT_CNTL_NBR, 1, 22))) = NVL(TRIM(ITF.CLIENT_USE_TXT), TRIM(SUBSTR(ITF.TRANSACTION_NO, 1, 22)))
WHERE TST.SECURITY_ADP_NBR = TTR.SECURITY_ADP_NBR
I fixed it by having all joins done by the join clause rather than in a where statement:
SELECT *
FROM TTR
INNER JOIN TST
ON TST.SECURITY_ADP_NBR = TTR.SECURITY_ADP_NBR
LEFT JOIN ITF
ON NVL(TRIM(TST.CLIENT_USE_TXT), TRIM(SUBSTR(TTR.PARENT_CNTL_NBR, 1, 22))) = NVL(TRIM(ITF.CLIENT_USE_TXT), TRIM(SUBSTR(ITF.TRANSACTION_NO, 1, 22)))
select pb.id,
p.name,
pb.batchName,
pb.batchCode,
s.detail,pb.program_id,
pb.session_id,
si.typeDescp,
si.id
from programBatch_info pb
join program p on pb.program_id=p.id
join session_info s on pb.session_id=s.id
join semester_info si on si.id=pb.semInfo_id
Here the name of first and last columns is 'id' so when I retrieve the values of this query first column object and last column object return the same value.But when I change the 'si.id' to 'pb.semInfo_id' the name of last column is'semInfo_id' and hence the correct values is retrieved. I tried this native query in hibernate platform.
Am I conceputally wrong or what is the actual case??
Not sure the question but try this:
select pb.id,
p.name,
pb.batchName,
pb.batchCode,
s.detail,pb.program_id,
pb.session_id,
si.typeDescp,
si.id as [si_id]
from programBatch_info pb
join program p on pb.program_id=p.id
join session_info s on pb.session_id=s.id
join semester_info si on si.id=pb.semInfo_id
This is the syntax to specify a new column name.