如何编写SQL查询以从两个表中获取数据

时间:2015-08-21 07:31:43

标签: java mysql hibernate jpa

@Entity
@Table(name = "campaigns")
@SqlResultSetMapping(name = "CampaignResultSet", classes = {
        @ConstructorResult(targetClass = Campaign.class, columns = {
                @ColumnResult(name = "campaign_sno", type = Long.class),
                @ColumnResult(name = "campaign_id", type = String.class),
                @ColumnResult(name = "campaign_title", type = String.class),
                @ColumnResult(name = "campaign_label", type = String.class),
                @ColumnResult(name = "campaign_description", type = String.class),
                @ColumnResult(name = "campaign_type", type = String.class),
                @ColumnResult(name = "email_subject", type = String.class),
                @ColumnResult(name = "from_name", type = String.class),
                @ColumnResult(name = "from_email", type = String.class),
                @ColumnResult(name = "to_name_type", type = String.class),
                @ColumnResult(name = "email_type", type = String.class),
                @ColumnResult(name = "email_status", type = Boolean.class),
                @ColumnResult(name = "api_key", type = String.class),
                @ColumnResult(name = "list_id", type = String.class),
                @ColumnResult(name = "created_by", type = Long.class),
                @ColumnResult(name = "created_date", type = Date.class),
                @ColumnResult(name = "updated_by", type = Long.class),
                @ColumnResult(name = "updated_date", type = Date.class) }) }, entities = {
                        @EntityResult(entityClass = Template.class, fields = {
                                @FieldResult(name = "templateSno", column = "template_sno"),
                                @FieldResult(name = "baseTemplateId", column = "base_template_id"),
                                @FieldResult(name = "templateName", column = "template_name"),
                                @FieldResult(name = "bodyHeader", column = "body_header"),
                                @FieldResult(name = "bodySubject", column = "body_subject"),
                                @FieldResult(name = "bodyContent", column = "body_content"),
                                @FieldResult(name = "bodyFooter", column = "body_footer"),
                                @FieldResult(name = "sourceCode", column = "source_code"),
                                @FieldResult(name = "modifiedCode", column = "modified_code"),
                                @FieldResult(name = "extractedCode", column = "extracted_code"),
                                @FieldResult(name = "createdBy", column = "created_by"),
                                @FieldResult(name = "createdDate", column = "created_date"),
                                @FieldResult(name = "updatedBy", column = "updated_by"),
                                @FieldResult(name = "updatedDate", column = "updated_date") }) })

我的查询从广告系列中选择* 。 在广告系列表格中,我的参考列名称为 template_id ,它将引用模板表格中的 template_sno 列。在检索广告系列时,我也需要模板数据。那么,如何编写查询以获取记录。

1 个答案:

答案 0 :(得分:1)

以下查询仅在数据相关时帮助您显示两个表中的所有字段。

SELECT * 来自竞选活动c JOIN模板t ON c.template_id = t.template_id;