I have created 2 tables in MySQL [items, orderList], the foreign key id in orderlist references the primary key id in items. Now I want to take all columns{id, name, price (in Items), and quantity (in orderList)} from 2 tables in Java, how can I show id once because when I query data it shows id from both tables?
You can do with join queries, try the below query and select the fields whatever you want from two tables
SELECT items.id, items.name, items.price, orderList.quantity
FROM items INNER JOIN orderList ON items.id = orderList.id
In order to fetch the data only once, you need to mention where it should come from. You can try the following:
SELECT I.ID, I.NAME, I.PRICE, O.QUANTITY FROM ORDERLIST O, ITEMS I WHERE I.ID = O.ID
Here we have given aliases to both the tables and we have mentioned that the ID column will be picked from the ITEMS table.
Related
I have a table which stores the orders. The orders table has an 'One to Many' mapping to another table, which stores ordered item, orderItems. For each row of the orderItems, it refers to a row in the Product table. And I have another table which stores product image, the product image table has a foreign key which points to the product. How can I use JPA specifications to join the product image in the query result of the orders?
Something like:
Order -> [orderId, ... orderItems[],...]
OrderItem -> [orderitemId, ... product,...]
Product -> [productId, productname... ]
ProductImage -> [producdImageId, .... productId...]
I am searching on Order table.
thanks!
I use Wordpress table to populate the javafx TableView.
In WP db I have 2 tables wp_posts & wp_postmeta and I need JOIN the table by ID (post_id in wp_postmeta) and select specific rows from wp_postmeta (not all).
For Exampe:
wp_postmeta structure post_id, meta_key, meta_value. In meta_key there is 3 rows - _yoast_wpseo_title, ._yoast_wpseo_metadesc, ._yoast_wpseo_focuskeywords
and I need this rows value from column meta_value.
My code:
ResultSet rs = connection.createStatement().executeQuery(" SELECT wp_postmeta._yoast_wpseo_title, wp_postmeta._yoast_wpseo_metadesc, wp_postmeta._yoast_wpseo_focuskeywords, wp_posts.post_title, wp_posts.post_name FROM wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id ");
And of course I receive error that columns _yoast_wpseo_title, _yoast_wpseo_metadesc, _yoast_wpseo_focuskeywords not found because thay are rows.
How I can do this? (I use java9, javafx, mysql jdbc). Or may be I must use PreparedStatement?
Screenshot:
Since you want to filter your query based on the content of the meta_key field, you use a WHERE clause:
SELECT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.post_title
FROM wp_postmeta INNER JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_postmeta.meta_key IN ('_yoast_wpseo_title', '_yoast_wpseo_metadesc', '_yoast_wpseo_focuskeywords')
You are providing a list of the meta keys you want to match. This will give (up to) three rows for each post.
If you want only one row for each post, things get more complicated (and probably aren't worth the trouble). You can use GROUP BY post_id and then study the Aggregate functions.
I have two tables:
Table1: orders
idOrder,
Blockquote
idUser
Table2: ordersinfo
idOrder,
.......,
.......
idOrder is primary a key for two tables. I have to delete from this tables rows by idUser. I tried different ways, but nothing helped me.
My Questions: What query, I should use?
I have this exception
MySQLIntegrityConstraintViolationException
You will need to issue two delete statement
-- Delete OrderInfo table
DELETE FROM ordersinfo
WHERE EXISTS (SELECT 1
FROM orders
WHERE orders.idOrder = ordersinfo.idOrder
AND IdUser = ???)
-- Delete Orders
DELETE FROM Orders
WHERE IdUser = ???
I have 2 tables TABLE1 and TABLE2.Table1 is having name and Table2 is having email and Phone.
To get the name,email and phone,I query as below
query = entityManagerUtil.createNativeQuery("select s.Name,c.Phone1,c.Email1 from Table1 s,Table2 c where c.id= s.NodeID and s.NodeID =21")
Now my next requirement is to update name,email and phone.As these parameters are present in different tables so I am searching for single query which will update 2 tables.Unfortunately I am using sql server and there is no way to update 2 tables using single query
So I am thinking to use #Transactional and 2 queries to update 2 tables like the follow
#Transactional
public void updateDetails()
{
Query query1= entityManagerUtil.entityManager.createNativeQuery("update Table1 set Name='' where id in (select NodeID from Table 2) and NodeID=21");
Query query2= entityManagerUtil.entityManager.createNativeQuery("update Table2 set Email='' and phone1='' where NodeID in (select id from Table 2) and NodeID=21");
query1.executeUpdate();
query2.executeUpdate();
}
Is there any other better way to update 2 tables?
you can use JDBCTemplate
http://sujitpal.blogspot.com.es/2007/03/spring-jdbctemplate-and-transactions.html
It allows to do multiple queries with one connection, so you save some time instead of doing it twice.
Why don't you use Hibernate entities for that. Just load the entities associated with Table1 and table2, modify them and let the automatic dirty checking mechanism to update the tables on your behalf. That's one reason for using an ORM by the way.
I am fetching records from my "record" table. "record" table has many columns tow of which are
client_id, foreign key mapping to client table.
creation_date , date of record creation
I would like to do a query on this table , but I would like to fetch only one record per client(latest creation_date record has preference).
Will following work?
select r.id,r.xx,r.yy
group by(r.client_id),r.creation_date
from record r
order by creation_date desc
I tried above and seems records fetched are not of latest creation dates.
Hope my question is clear
Just keep your query and add a WHERE condition :
SELECT r.id,r.xx,r.yy
GROUP BY(r.client_id)
FROM record r
WHERE r.creation_date = (SELECT MAX(creation_date) FROM record tmp WHERE tmp.client_id = r.client_id )
Take a look at This discussion
This should give you a good starting point in HQL.
from Record as r inner join fetch r.client
where r.creation_date > (
select max(rec.creation_date) from Record rec
where rec.client.client_id = r.client.client_id
)
This of course assumes that your Record has a reference to its parent Client called client.