搞乱连接,我想获得2个表数据

时间:2017-07-22 16:30:07

标签: sql join inner-join firebird

EDITED !!

我努力从桌子上获取数据,但我没有运气。

我的查询

id

我的代码表是

SELECT CODES.CODE, WARE.QTY
FROM   CODES 
LEFT JOIN WARE ON WARE.ID = CODES.ID
GROUP  BY CODES.CODE , WARE.QTY
ORDER BY CODES.CODE

我的商品表是

CODE          |ID
--------------|-----------
1402201700006 | 123  
1102201700006 | 124  

我的结果是

QTY           |ID
--------------|-----------
12            | 123
1             | 123
2             | 124
1             | 124

而不是

CODE          |QTY
--------------|---------
123           |12
123           |1
124           |2
124           |1

1 个答案:

答案 0 :(得分:0)

我认为这是因为“仓库”表中“items”表中的每个“id”都有多行。

所以你可以使用下面的查询:

@Entity
@Table(name="User")
public class User 
{

//data members
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer userId;
@Column(length = 30)
@NotEmpty(message="UserName connot be null")
private String userName;
@Range(min=6,max=12)
@NotEmpty(message="Password connot be null")
private String password;

@Column(length = 40,unique=true)
@NotEmpty(message="Email connot be null")
@Email(message="Invalid Email")
private String email;
@NotEmpty(message="gender field is must")
@Column(length = 15)
private String gender;
@Column(length = 15)
private String verification;
@OneToOne(mappedBy="user", cascade=CascadeType.ALL)
private UserAddress useradd;

/*

@NotEmpty(message="phone number cannot be null")
private String phoneNo;
@NotEmpty(message="Address connot be null")
private String address;
private String shippingInfo;
*/


//constructors
public User()
{
    super();
}
public User(String userName, String password, String email, String gender) {
    super();
    this.userName = userName;
    this.password = password;
    this.email = email;
    this.gender = gender;
}




//getter and setter
public String getGender() 
{
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}


public String getVerification() {
    return verification;
}
public void setVerification(String verification) {
    this.verification = verification;
}





public Integer getUserId() {
    return userId;
}
public void setUserId(Integer userId) {
    this.userId = userId;
}


public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}


public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}



public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}


//toString
public UserAddress getUseradd() {
    return useradd;
}
public void setUseradd(UserAddress useradd) {
    this.useradd = useradd;
}
@Override
public String toString() {
    return "User [userId=" + userId + ", userName=" + userName + ", 
    password=" + password + ", email=" + email
    + ", gender=" + gender + ", verification=" + verification + "]";
}

}

@Entity
@Table(name = "user_adr")
public class UserAddress {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(length = 400)
@NotEmpty(message="Address can't be Empty")
private String address;

@OneToOne
@JoinColumn(referencedColumnName="userId")
private User user;

@Embedded
private Phone myPhone;

public UserAddress() {
    // TODO Auto-generated constructor stub
}



public UserAddress(String address) {
    super();
    this.address = address;

}



public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}



public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public Phone getMyPhone() {
    return myPhone;
}

public void setMyPhone(Phone myPhone) {
    this.myPhone = myPhone;
}

}
相关问题