• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

how to set PreparedStatement if the select query contains id of 1st table = id of 2nd table ?

 
Ranch Hand
Posts: 570
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to seek advice on a query "

Select t1.a, t2.b, t2.c from t1 join t2 on t1.id = t2.id where t1.a = ?, t2.b = ?, t2.c = ?

My questions :

1) if I do not know the id, should I put a ? in the query
2) do I need to set prepared statement for id ?

Thanks in advance.
 
author & internet detective
Posts: 39587
781
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't care what the id is, you don't need a ?. It's only if you want t a specific value.

The ? you have for the other values are good.
 
tangara goh
Ranch Hand
Posts: 570
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:If you don't care what the id is, you don't need a ?. It's only if you want t a specific value.

The ? you have for the other values are good.



Thanks Jeanne.

BTW, is it possible to add group by id at the end and it will make the data less duplicated ?
 
Jeanne Boyarsky
author & internet detective
Posts: 39587
781
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding group by won't help because then you'd have to use an aggregate. If you are worried about duplication, add a distinct to your select clause. I don't know how much duplication you'd have though. Id sounds pretty unique.
 
tangara goh
Ranch Hand
Posts: 570
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Adding group by won't help because then you'd have to use an aggregate. If you are worried about duplication, add a distinct to your select clause. I don't know how much duplication you'd have though. Id sounds pretty unique.



Hi Jeanne,

It is not adding Select distinct on t1.1a right ?

Select t1.a, t2.b, t2.c from t1 join t2 on t1.id = t2.id where t1.a = ?, t2.b = ?, t2.c = ?

I have use distinct on t1.id = t2.id but then it gives me error...

so, I am not sure where about to put Select distinct.

Could you kindly let me know ?

Tks.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!