My sql query looks like:
select T.Col1
from (
select Past_Diagnoses_1 as Col1
from patienthistory
union
select Past_Diagnoses_2 as Col1
from patienthistory
union
select Past_Diagnoses_3 as Col1
from patienthistory
union
select Past_Diagnoses_4 as Col1
from patienthistory
) as T;
I keep getting an error on Spring which says No value specified for parameter 2
I am trying to union 4 columns together into 1 super long column.
This is a simplified code. I have 32 columns but for question I have shown 4
Java code :
public UserTemp findHistoryByID(Integer Patient_Number) {
String sql = "select T.Col1\n" +
"from (\n" +
" select Past_Diagnoses_1 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_2 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_3 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_4 as Col1\n" +
" from patienthistory\n" +
" union\n" +
" select Past_Diagnoses_5 as Col1\n" +
" from patienthistory\n"
" where Patient_Number = ?\n" +
" ) as T";
return jdbcTemplate.queryForObject(sql, new Object[]{Patient_Number}, (rs, rowNum) ->
new UserTemp(
rs.getString("T.Col1")
));
}
I am trying to use a CASE statement in MySQL based on a variable. I tried the code below; however, I have found that "The SELECT statement cannot refer to system variables or user-defined variables". What I am trying to achieve is that I have an input date. I get its day of week so I know which day it is on (1 is Sunday). I then only want rows returned in the query that have the day set to "Y" (e.g., if day of week is 1 then stt_sunday is "Y"). The table has:
stt_id
stt_start_date
stt_end_date
stt_start_time
stt_end_time
stt_sunday
stt_monday
stt_tuesday
stt_wednesday
stt_thursday
stt_friday
stt_saturday
My code is:
String selectQry1 = (" SET #clickedDateDOW = clickedDateDOW; " +
" SELECT st_type, csm_session_timetable.stt_id, stt_start_date, stt_end_date, stt_start_time, stt_end_time " +
" FROM csm_session_type, csm_session_timetable " +
" WHERE csm_session_type.cli_id = ? " +
" AND csm_session_type.prm_id = ? " +
" AND csm_session_type.st_id = csm_session_timetable.st_id " +
" AND (csm_session_timetable.stt_end_date IS NULL || csm_session_timetable.stt_end_date > ?) " +
" AND csm_session_timetable.gym_id = ? " +
" AND CASE " +
" WHEN #clickedDateDOW = 1 AND stt_sunday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 2 AND stt_monday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 3 AND stt_tuesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 4 AND stt_wednesday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 5 AND stt_thursday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 6 AND stt_friday LIKE 'Y' THEN 1 " +
" WHEN #clickedDateDOW = 7 AND stt_saturday LIKE 'Y' THEN 1 " +
" ORDER BY stt_start_date DESC, stt_start_time, stt_end_date DESC;");
hai i am getting products details from db while exceuting the flexiblesearchservice.search(query) it is throwing this error.my flexible search query is working fine with hac. but throwing error from flexible search service. my query and service is shown below.
"({{select {p:"+ProductModel.PK+"} from { "+ OrderModel._TYPECODE +" as o " +
" join "+ OrderStatus._TYPECODE+" as os on {os:pk} = {o:"+OrderModel.STATUS+"}"+
" join "+ OrderEntryModel._TYPECODE +" as oe on {oe:"+ OrderEntryModel.ORDER +"} = {o:"+OrderModel.PK+"}"+
" join "+ ProductModel._TYPECODE +" as p on {oe:"+OrderEntryModel.PRODUCT+"} = {p:"+ProductModel.PK+"}"+
" join "+ CatalogVersionModel._TYPECODE+" as cv on { p:"+ProductModel.CATALOGVERSION +"} = {cv:"+CatalogVersionModel.PK+"}"+
" join "+ CatalogModel._TYPECODE +" as c on {p:"+ProductModel.CATALOG+"} = {c:"+CatalogModel.PK+"}} "+
"where {os:code}='"+OrderStatus.COMPLETED +"' and {"+OrderModel.DATE+"}>'2017-08-16 00:00:00.000' " +
"and {"+OrderModel.DATE+"}<'2020-09-30 00:00:00.000' " +
"and {cv:"+CatalogVersionModel.VERSION+"}= 'Online' " +
"and {c:"+CatalogModel.ID+"} like 'apparelProductCatalog' " +
"group by {p:"+ProductModel.PK+"} order by sum({oe:"+OrderEntryModel.QUANTITY+"}) desc" +
" limit 10 }})";
final FlexibleSearchQuery fQuery = new FlexibleSearchQuery(QUERY);
fQuery.setResultClassList(Arrays.asList(ProductModel.class));
final SearchResult<ProductModel> searchResult =flexibleSearchService.search(fQuery);
final List<ProductModel> productModelList= searchResult.getResult();```
**error:** unable to evaluate the expression method threw 'java.lang.illegalargumentexception' exception
please help me out.
"where {os:code}='"+OrderStatus.COMPLETED +"'
This above code may be the issue, OrderStatus.COMPLETED returns an object, not String.
You can pass it as a query parameter.
"where {os:code} = '" + OrderStatus.COMPLETED.getCode() +"'
I am trying to use a prepared statement to copy one row to a new row (INSERT)in the same table and include some static values. However, I get the following error:
SQLException in copyProgram: java.sql.SQLException: Operand should contain 1 column(s)
My input is:
prId: 4 accountID: 50 newFromDate: 2020-04-27 newToDate: 2020-04-28
My code is:
String insertQry = ("INSERT INTO at_program "
+ "(acc_id, pr_name, pr_start_date, pr_start_time, pr_end_date, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " (SELECT (?, pr_name, ?, pr_start_time, ?, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " FROM at_program "
+ " WHERE pr_id = ?);");
ps = c.prepareStatement(insertQry, Statement.RETURN_GENERATED_KEYS);
// Create a statement and execute the query on it
ps.setString(1, accountID);
ps.setString(2, fromDate);
ps.setString(3, toDate);
ps.setString(4, prID);
ps.executeUpdate();
You have parens around the field list in your SELECT! Remove them.
The issue is that SELECT expects a list of (individual) columns, but you are passing a multi-column value.
SELECT x, y, z sees x, y and z as three values each consisting of one column, yet you are passing SELECT (x, y, z) which is interpreted as one value with three columns, hence the error.
By the way, the parens around the whole SELECT are superfluous as well, this time because INSERT INTO table (columns) SELECT ... is a recognized syntax construct in itself.
Corrected code:
String insertQry = ("INSERT INTO at_program "
+ "(acc_id, pr_name, pr_start_date, pr_start_time, pr_end_date, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes) "
+ " SELECT ?, pr_name, ?, pr_start_time, ?, pr_end_time, "
+ "pr_cost_pp, pr_woodbeads, pr_special, pr_joeys, pr_cubs, "
+ "pr_scouts, pr_venturers, pr_rovers, pr_leaders, pr_family, "
+ "pr_swimming, pr_pioneering, pr_archery, pr_canoe, pr_bushwalking, "
+ "pr_4wd, pr_abseiling, pr_snorkelling, pr_boating, "
+ "pr_rock_climbing, pr_caving, pr_branch_instructions, pr_policies_information, "
+ "pr_whs, pr_other, pr_notes "
+ " FROM at_program "
+ " WHERE pr_id = ?;");
how to write this query in hibernate query language
select * from preferred_space p, building b, floor f, space_type st, space s
where p.user_id=11
and p.space_id=s.id
and s.building_id=b.id
and s.floor_id=f.id
and s.space_type_id=st.id
order by p.id;
If I do it this way it is showing me an error?
String sql = "from PreferredSpace p, Space s, Building b, Floor f, SpaceType st " +
"where p.userId = ? " +
"and p.spaceId = s.id" +
"and s.buildingId = b.id" +
"and s.floorId = f.id" +
"and s.spaceTypeId = st.id" +
"order by p.id";
error:
ERROR o.h.hql.internal.ast.ErrorCounter - line 1:242: unexpected token: s
16:18:17.569 [http-bio-8080-exec-24] ERROR o.h.hql.internal.ast.ErrorCounter - line 1:242: unexpected token: s
antlr.NoViableAltException: unexpected token: s
shows the same error for every row that starts with "s." and also for "by".
You need to add spaces:
String sql = "from PreferredSpace p, Space s, Building b, Floor f, SpaceType st " +
"where p.userId = ? " +
"and p.spaceId = s.id " +
"and s.buildingId = b.id " +
"and s.floorId = f.id " +
"and s.spaceTypeId = st.id " +
"order by p.id";