I have this query using hibernate:
#Query(value = "BEGIN SET #rank = 1; CREATE TEMPORARY TABLE IF NOT EXISTS results "
+ "SELECT User.userId, User.username, #rank FROM User WHERE User.username = :searchString ;"
+ "SET #rank = 2; INSERT INTO results (SELECT User.userId, User.username, #rank FROM Following "
+ "JOIN User ON Following.followsId = User.userId " + "JOIN UserProfile ON UserProfile.userProfileId = User.userProfileId "
+ "WHERE Following.userId = :userId AND User.username LIKE :searchString "
+ "ORDER BY UserProfile.followsCount DESC); "
+ "SET #rank = 3; INSERT INTO results (SELECT User.userId, User.username, #rank FROM User "
+ "JOIN UserProfile ON UserProfile.userProfileId = User.userProfileId "
+ "WHERE User.username LIKE :searchString ORDER BY UserProfile.followsCount DESC); "
+ "SELECT User.* FROM results JOIN User ON User.userId = results.userId ORDER BY #rank DESC LIMIT 50; "
+ "DROP TEMPORARY TABLE results; END;", nativeQuery = true)
List<User> findLike(#Param("searchString") String searchString, #Param("userId") Long userId);
When I run it, i'm told that it could not extract the result set (by JUNIT) and in the console
16:32:24.554 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET #rank = 1; CREATE TEMPORARY TABLE IF NOT EXISTS results SELECT User.userId, ' at line 1
I turned hibernate.show_sql=true and even ran the query that was logged to the console using mysqlworkbench and it works.
Note that this query is running in a interface that is defined as such:
public interface IUserJpaDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User>
Any ideas why it works in MySqlWorkbench and not in hibernate?
Related
For some reasons my query is running perfectly in MySQL WorkBench but when added to the repository I am getting syntax error
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'as e1, profile_history as e2 WHERE e2.profile_id =
e1.profile_id and' at line 1.
#Query(value = "( SELECT e2.* "
+ " FROM (select ph1.profile_id, max(ph1.last_updated_on) as last_updated_on2 "
+ " FROM profile_history as ph1 "
+ " GROUP BY ph1.profile_id ) as e1, profile_history as e2 "
+ " WHERE e2.profile_id = e1.profile_id and e2.last_updated_on = e1.last_updated_on2 )", nativeQuery = true)
Page<ProfileHistory> getAllProfileHistoryByLastestRow(Pageable paging);
Honestly expecting to get a db hit without any error and retrieve results.
Maybe you need to specify dialect in the properties file. Don’t know your MySQL version, so can’t give you concrete example, but it should be something like this:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
I figured out the issue after fighting for 02 days. The jdbc driver is not interpreting the query correctly. I had to escape the columns and tables in the native query. Normally I shouldn't have to do this.
#Query(value = " SELECT `e2`.`*` "
+ " FROM (select `eph1`.`employee_id`, max(`eph1`.`last_updated_on`) as `last_updated_on2` "
+ " FROM `employee_profile_history` `eph1` "
+ " GROUP BY `eph1`.`employee_id` ) `e1`, `employee_profile_history` `e2` "
+ " WHERE (`e2`.`employee_id` = `e1`.`employee_id` and `e2`.`last_updated_on` = `e1`.`last_updated_on2` )",
countQuery = "SELECT distinct count(*) FROM `employee_profile_history`",
nativeQuery = true)
I am developing a sequence in MySQL & VB.NET in which, when deleting a record, the number that identifies it, is deleted and put a sequence of 1 to 'n', the sequence is as follows.
this is the instruccion for MySQL.
SET #rownum=0;
UPDATE id_line t, (SELECT #rownum:=#rownum+1 rownum, id_line.* FROM id_line WHERE id_line.line_name=59999 and id_line.line_no<>0) r
SET t.line_no = r.rownum
WHERE (t.id_line_b = r.id_line_b)
in VB.net I use this
cmdB = New MySqlCommand("SET #rownum=0 UPDATE id_line t, (SELECT #rownum=:#rownum+1 rownum, id_line.* FROM id_line WHERE id_line.line_name='" & TextBox1.Text & "' and id_line.line_no<>0) r ) SET t.line_no = r.rownum WHERE(t.id_line_b = r.id_line_b)", conn)
but VB sends this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE id_line t, (SELECT #rownum=:#rownum+1 rownum, id_line.* FROM id_lin' at line 1
Could you help me please in this error?
Updated 1:
This is my string connection.
Public conString As String = "Data Source=server_one;port=3306;Initial Catalog=test_db;User Id=root;password=root;Allow User Variables=True"
other better way for
create mysql procedure for delete mysql data query
https://www.tutorialspoint.com/What-is-stored-procedure-and-how-can-we-create-MySQL-stored-procedures
I found the error, a parenthesis, it was misplaced, this is the correct code.
cmdB = New MySqlCommand("SET #rownum:=0; UPDATE id_line t, (SELECT #rownum:=#rownum+1 rownum, id_line.* FROM id_line WHERE id_line.line_name='" & TextBox1.Text & "' and id_line.line_no<>0) r SET t.line_no = r.rownum WHERE(t.id_line_b = r.id_line_b)", conn)
Thank you everyone for your attention and support.
Regards.
Where does the SET assignment (SET #running_total_available := 0;) go when using dynamic SQL as in my query below?
My query works fine as shown, but the "#running totals..." crash the query when I use SET #running_total_available := 0
and reset each row to "0" without the use of SET.
I'm stumped. Thanks!
$sql = "
SELECT
SQL_CALC_FOUND_ROWS
#running_total_available := 0,
product_source,
username,
product_short_name,
noci_count,
item_listing_id,
IF(num_purchased=0,'', num_purchased) AS num_purchased ,
IF(num_available=0,'', num_available) AS num_available,
IF(num_sold=0,'', num_sold) AS num_sold,
i.tot_items,
date(date_listing_noci) AS date_listing_noci,
IFNULL(date(date_listing_removed),'') AS date_listing_removed,
#running_total_purchased:=#running_total_purchased + i.num_purchased AS running_total_purchased,
#running_total_available:=#running_total_available + i.num_available AS running_total_available,
#running_total_sold:=#running_total_sold + i.num_sold AS running_total_sold,
#running_total_item:=#running_total_item + i.num_sold + i.num_available AS running_total_item,
#running_total_noci:=#running_total_noci + i.noci_count AS running_total_noci
FROM
(SELECT
product_source,
item_listing_id,
p.product_short_name,
username,
noci_count,
IF(num_purchased=0,0,num_purchased) AS num_purchased,
IFNULL(num_available,0) AS num_available,
IFNULL(num_sold,0) AS num_sold,
IFNULL(num_sold,0) + IFNULL(num_available,0) AS tot_items,
date(date_listing_noci) AS date_listing_noci,
IFNULL(date(date_listing_removed),'') AS date_listing_removed
FROM `investigation`
JOIN product p USING (product_id)
WHERE 1 ";
*** dynamic constructs here ***
# close query
$sql.= " ) i ";
$result = $objDbMysqli->query($sql);
Initialize it in a cross-joined subquery:
SELECT
...
FROM (
...
JOIN product p USING (product_id)
WHERE 1
) i
CROSS JOIN (select #running_total_available := 0) initvars
Use it on your own risk because the execution order is not guaranteed.
I have a select statement where I'm pulling data from Database1 that looks something like this...
g_SQL = "SELECT 'ID' = companyID, 'Name' = (CompanyName + CompanyAddress)
g_SQL = " FROM tblCompanies WHERE CompanyStatus='InBusiness'"
Now what I need to do is go into another database and select data from tblCompanyinventory, Database2 where I need to do a...
sum(inventory) for each CompanyID
And then I'd want it to be displayed in the 'Name' like this 'Name'= (CompanyName + CompanyAddress + sum(inventory))
I will use the 2 fields: ID and NAME and I pass it on to a combobox loader. This is a vb6 application I'm working on.
Any ideas how I'd go about it? Open Rowset? I've never done anything of this nature before.
I have connections set for both - for Database1 it's g_CN1, and Database2 it is g_CN2 - as ADODB.Connection. These databases are on the same server.
Based on OP's comments some SQL like this may be what is required.
g_SQL = "SELECT 'ID' = C.companyID, 'Name' = (CompanyName + CompanyAddress), I.InvSum "
g_SQL = g_SQL + " FROM Database1.dbo.tblCompanies C JOIN (SELECT SUM(inventory) As InvSum from"
g_SQL = g_SQL + " Database2.dbo.tblCompanyInventory GROUP BY CompanyId) I "
g_SQL = g_SQL + " ON I.CompanyId = C.companyId WHERE CompanyStatus='InBusiness'"
without knowing how the connection is established I can't say whether there will be an authentication issue or not.
I have a query like this:
SET #a = (SELECT GROUP_CONCAT(Id) FROM MyTable1 WHERE Id < 10);
SELECT * FROM MyTable2 WHERE find_in_set(IdLite, #a);
SELECT * FROM MyTable3 WHERE find_in_set(IdLite, #a);
SELECT * FROM MyTable4 WHERE find_in_set(IdLite, #a);
I've tryed to use this code to get resut:
Using ds As DataSet = MySqlHelper.ExecuteDataset(CnStr, SqlStr)
but I get error:
Fatal error encountered during command execution.
Error message is:
Parameter '#a' must be defined.
I've also tryed:
SELECT * FROM MyTable2 WHERE find_in_set(IdLite,
#a := (SELECT GROUP_CONCAT(Id) FROM MyTable1 WHERE Id < 10));
SELECT * FROM MyTable3 WHERE find_in_set(IdLite, #a);
SELECT * FROM MyTable4 WHERE find_in_set(IdLite, #a);
but I get the same error.
What's the correct way to get result into a DataSet?
DataSet mydataset = new DataSet();
MySqlConnection myConnection = new MySqlConnection();
myConnection.ConnectionString = "************";
myConnection.Open();
string mySelectQuery = "SELECT * FROM table";
MySqlCommand myCommand = new MySqlCommand(mySelectQuery,myConnection);
MySqlDataAdapter adapter = new MySqlDataAdapter(myCommand);
adapter.Fill(mydataset, "table");
dataGridView1.DataSource = mydataset;
dataGridView1.DataMember = "table";
myConnection.Close();
You can have a look in the following links:
http://forums.codeguru.com/showthread.php?448008-How-do-i-load-mysql-data-into-a-dataset-then-into-a-datagrid
http://www.dotnetheaven.com/article/how-to-load-data-from-database-into-datagridview-in-vb.net
If my answer is correct then please masrk as correct. Thank you
The error is in the connection string.
The solution is to add ;Allow User Variables=True to the database name.
This way:
CnStr = "datasource=" + Server_Name + _
";username= " + UserDB + _
";password=" + Password + _
";database=" + Database_Name + ";Allow User Variables=True"