我有以下两个表 -
姓名年龄城市
21德里
B 23孟买
C 35 Pune
名称属性Attribute_value
电话999999999
护照A12345
位置中国
技能开发人员
B电话8888888888
B技能测试员
现在我想创建表3,在那里我得到以下细节 -
姓名年龄城市电话护照位置技巧
注意 - attribute_values应该包含在电话,护照,位置和技能标题下。 表3中每个“名称”应该有一行。
[假设Table2中的属性列中只有4个不同的值,并且对于无法找到某个属性的Name,可以将attribute_value假定为NULL]
答案 0 :(得分:1)
如果只有4
个属性,那么您可以使用以下内容。
注意:我假设这是您正在进行的测试场景。在实际数据库中,name
对于密钥不是一个好主意,因此您无法根据name
select t1.*, t2.*
from
table1 t1
left join
( select name,
max(case when attribute = 'Phone'
then attribute_value end) as Phone ,
max(case when attribute = 'Passport'
then attribute_value end) as Passport,
max(case when attribute = 'Location'
then attribute_value end) as Location,
max(case when attribute = 'Skills'
then attribute_value end) as Skills
from table2
group by name
) t2
on t1.name=t2.name
左连接仅返回null
name
,table2
不在inner join
。如果您不想要这些,请使用package com.example.zaid_pc.mpd2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class tournament extends AppCompatActivity {
private EditText etTournament;
private EditText emailTournament;
private DatabaseReference mDatabaseRefernce;
Button btnTournament;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tournament);
etTournament = (EditText)findViewById(R.id.etTournament);
emailTournament = (EditText)findViewById(R.id.emailTournament);
btnTournament = (Button)findViewById(R.id.btnTournament);
mDatabaseRefernce = FirebaseDatabase.getInstance().getReference().child("People Registered");
btnTournament.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String name = etTournament.getText().toString().trim();
final String email = emailTournament.getText().toString().trim();
//mDatabaseRefernce.child("Name").setValue(name);
//mDatabaseRefernce.child("Email").setValue(email);
final DatabaseReference peopleRegisered = mDatabaseRefernce.push();
peopleRegisered.child("Name").setValue(name);
peopleRegisered.child("Email").setValue(email);
Toast.makeText(tournament.this, "You will be notified via email", Toast.LENGTH_LONG).show();
finish();
}
});
}
。