I have a database that currently got 3 tables that i want to join together.
products
product_category
category
I have an sql join that does what i need it to do, which is taking category_id and returning all products in that category.
SELECT products.name FROM products
JOIN product_category ON products.id = product_category.product_id
JOIN categories ON categories.id = product_category.category_id
WHERE categories.id = ?
I have tried to google for a while now without success.
How can i make this happen?
Related
I have two queries that I'm trying to join together.
In first_query TABLE2.PROCESS_ID, every PROCESS_ID is unique in that table. In second_query though there are several PROCESS_ID's with the same number in TABLE3, so I think I have to do a one-to-many join. The join_query I have is giving me an error ORA-00933: SQL command not properly ended which I'm assuming has something to do with the one-to-many relationship with the JOIN.
I'm not really sure how to resolve this. Any help would be much appreciated!
first_query = """
SELECT TABLE1.RULE_ID, TABLE2.STATUS, TABLE2.ERROR_MESSAGE, TABLE2.PROCESS_ID
FROM TABLE2 LEFT JOIN
TABLE1
ON TABLE1.RULE_ID = TABLE2.RULE_ID
WHERE TABLE1.RULE_NAME IN ('TEST1', 'TEST2')
"""
second_query = """
SELECT RECORDS_PROCESSED, PROCESS_ID, STATUS
FROM TABLE3
"""
join_query = """
SELECT RULE_ID, STATUS, ERROR_MESSAGE, PROCESS_ID
FROM (first_query) as query_1
INNER JOIN (second_query) as query_2
ON query_1.PROCESS_ID = query_2.PROCESS_ID
GROUP BY PROCESS_ID desc
"""
You can not select 4 columns and group by only one of them unles you include selected columns as part of aggregation fucntion(like max(), sum(),...). One of the options is this:
SELECT query_1.RULE_ID --1
, query_2.STATUS --2
, query_1.ERROR_MESSAGE --3
, query_1.PROCESS_ID --4
FROM (SELECT TABLE1.RULE_ID
, TABLE2.STATUS
, TABLE2.ERROR_MESSAGE
, TABLE2.PROCESS_ID
FROM TABLE2
LEFT JOIN TABLE1
ON TABLE1.RULE_ID = TABLE2.RULE_ID
WHERE TABLE1.RULE_NAME IN ('TEST1', 'TEST2')) query_1
INNER JOIN (SELECT RECORDS_PROCESSED
, PROCESS_ID
, STATUS
FROM TABLE3) query_2
ON query_1.PROCESS_ID = query_2.PROCESS_ID
GROUP BY query_1.RULE_ID
, query_2.STATUS
, query_1.ERROR_MESSAGE
, query_1.PROCESS_ID
Also please do consider using aliases like this(in your first query):
SELECT T1.RULE_ID
, T2.STATUS
, T2.ERROR_MESSAGE
, T2.PROCESS_ID
FROM TABLE2 T2
LEFT JOIN TABLE1 T1 ON T1.RULE_ID = T2.RULE_ID
WHERE T1.RULE_NAME IN ('TEST1', 'TEST2')
Also, apply the same logic with aliases on your final query or else you will have a different kind of error : "ORA-00918: column ambiguously defined"
Here is a small demo
CTE (i.e. the WITH factoring clause) might help.
WITH first_query
AS (SELECT table1.rule_id,
table2.status,
table2.error_message,
table2.process_id
FROM table2 LEFT JOIN table1 ON table1.rule_id = table2.rule_id
WHERE table1.rule_name IN ('TEST1', 'TEST2')),
second_query
AS (SELECT records_processed, process_id, status FROM table3)
SELECT a.rule_id,
a.status,
a.error_message,
a.process_id
FROM first_query a INNER JOIN second_query b ON a.process_id = b.process_id
GROUP BY you used is invalid; you can't group results by only one column. If results have to be unique, use select distinct. If you have to use group by, specify all columns returned by select (which leads you back to what distinct does), or see whether some column(s) have to be aggregates - in that case, group by makes sense.
Also, you should always use table aliases. Without them, query is invalid as database engine doesn't know which table those columns (if they share the same name) belong to.
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.
How do I change the query which is created by hibernate between two related tables?
For example, Hibernate creates a query for a relationship between two tables using a WHERE clause but I need a relationship between them using a JOIN clause instead. So I need to change the Hibernate query from:
SELECT supportdoc1_.TYP_FILE_SUPDC
FROM FUND.SUPPORT_REQUEST this_,
FUND.SUPPORT_DOCUMENTS supportdoc1_
WHERE this_.SUPDC_SEQ_SUPDC_SUPDC = supportdoc1_.SEQ_SUPDC_SUPDC
AND this_.TYP_STATUS_SUPRQ <> 4
AND 1 = 1
ORDER BY supportdoc1_.TYP_FILE_SUPDC DESC
To:
SELECT supportdoc1_.TYP_FILE_SUPDC
FROM FUND.SUPPORT_REQUEST this_
LEFT JOIN
FUND.SUPPORT_DOCUMENTS supportdoc1_
ON (SEQ_SUPDC_SUPDC = SUPDC_SEQ_SUPDC_SUPDC)
WHERE this_.TYP_STATUS_SUPRQ <> 4
AND 1 = 1
ORDER BY supportdoc1_.TYP_FILE_SUPDC DESC
Thanks.
I've successfully created some JPA 2 criteria queries, but im now converting a query that is too complex for me to be able to convert it. Anyone up for a challenge? I will be eternally thankful for any response! :)
This is the query in plain SQL (mysql):
SELECT o.orderId, oi.insertDate
FROM AIDA_ORDER o
LEFT OUTER JOIN
(SELECT orderId, MIN(insertDate) AS insertDate FROM AIDA_ORDER_INSERT
WHERE insertDate > CURDATE() GROUP BY orderId) AS oi
ON oi.orderId = o.orderId
ORDER BY oi.insertDate, o.orderId;
The AIDA_ORDER table corresponds to a OrderBean, and AIDA_ORDER_INSERT to a OrderInsertBean.
A AIDA_ORDER can have multiple AIDA_ORDER_INSERTS.
Hi I´m using Eclipselink and I did a native query to select some fields of 2 tables. I mapped my table Logins in a model class. I would not like to map my table "B" because I need only 2 fields of this table on my sql result.. can I map this 2 fields in my Logins table to my sql result ?
My sql is this:
select l.login_id, s.lugarcerto,s.vrum, l.username, l.first_name, l.last_name, l.phone, l.fax_number, l.address, l.zip,
l.address2 as 'birth_date', l.city as 'cpf_cnpj'
from Logins l
join (select se.login_id, lugarcerto = min(case when se.service = 'IM' then '1' end), vrum = min(case when se.service = 'VE' then '1' end)
from (select distinct ad.login_id, substring(ap.Rate_code,(CHARINDEX('-', ap.Rate_code)+1),2) as 'service'
from Ad_Data.dbo.ad ad
join Ad_Data.dbo.ad_pub ap on (ad.ad_id = ap.ad_id)
where ap.ad_type =1) se
group by se.login_id) s on (s.login_id = l.login_id)
I did map Logins table and I want to map s.lugarcerto and s.vrum to my SQL query result.
There´s anyway to just add it to my Logins model ?
Not without having mappings for the attributes you want those values put into, and not without causing problems with them being cached in the entity.
Why not just return the values beside the entity, much like you would with a JPQL query such as: "Select l, subquery1, subquery2 from Logins l" ie:
Query q = em.createNativeQuery(yourQueryString, "resultMappingName");
And in the entity, include the annotation:
#SqlResultSetMapping(name="resultMappingName",
entities={#EntityResult(entityClass=com.acme.Logins.class, )},
columns={#ColumnResult(name="LUGARCERTO"), #ColumnResult(name="VRUM")}
)
Best Regards,
Chris