zwwcn

Just another WordPress.com site

Monthly Archives: July 2011

mysql can’t specify target table for update in FROM clause

In MySQL, you can’t modify the same table which you use in the SELECT part.

 

If you absolutely need the subquery, there’s a workaround

 

UPDATE tbl SET col = (
  SELECT * FROM (SELECT.... FROM) AS x);

The nested subquery in the FROM clause creates an implicit temporary table, so it doesn't count as the same table you're updating
Advertisements

Hibernate HQL like query

What actually worked was putting the “%” signs in the parameter:

suggestedCustomers = em.createNamedQuery(
“Customer.FindByAgencyAndCustomerName”).setParameter(
“agency”, currentUser.getAgency()).setParameter(
“customerNameLike”, “%”+ suggestionObject + “%”).getResultList();

 

@NamedQuery(name = “Customer.FindByAgencyAndCustomerName”, query = “SELECT a FROM Customer a WHERE a.recStatus = ‘A’ AND a.agency = :agency AND a.customerName LIKE :customerNameLike )