如何从多个表中获取一个字段的数据?

时间:2017-08-14 15:24:09

标签: sql sql-server

我在表中有一列ContentID,用于标识其他表中存在的内容。例如,它可以引用pageIDproductID等。我的计划是使用它来提取我需要的其他信息,例如页面名称或产品名称。这就是我到目前为止所做的:

SELECT TL.ID, TL.TableName, TL.FileName, TL.ContentID, p.PageName AS Content
FROM TranslationLog TL
LEFT JOIN Pages P ON TL.ContentID = P.PageID
LEFT JOIN Categories C ON TL.ContentID = C.CategoryID
LEFT JOIN ProductDescriptions PD ON TL.ContentID = PD.SKU 

想法是针对每一行,我想使用TableNameContentID字段获取指定内容的数据。目前,我可以选择PageName来获取p.PageName AS Content。但是,我想为每个表格执行此操作;如果行对应于pages表,则查询该表 - 对于类别和产品描述也是如此。我还需要它来使用别名“Content”,无论我们使用的是哪个字段,例如PageNameProductName

是否可以这样做?

编辑:

rd_nielsen发布的解决方案几乎是完美的,但事实证明与ContentID实际上有点重叠。以下是我最终解决的问题:

SELECT TL.ID, TL.TableName, TL.FileName, TL.ContentID, coalesce(P.PageName, C.CategoryName, PD.ProductName)
FROM TranslationLog TL
LEFT JOIN Pages P ON TL.ContentID = P.PageID AND TL.TableName = 'Pages'
LEFT JOIN Categories C ON TL.ContentID = C.CategoryID AND TL.TableName = 'Categories'
LEFT JOIN ProductDescriptions PD ON TL.ContentID = PD.SKU AND TL.TableName = 'Products'

3 个答案:

答案 0 :(得分:2)

您应该使用交叉申请。例如:

select 
    T.* 
from 
    table_stored_data u
cross apply dbo.created_function(u.contentid,u.tablename,u.search_column) T

你需要为它编写一个函数。

答案 1 :(得分:1)

如果TranslationLog.ContentID的值只能出现在其中一个相关表中,那么您可以合并这些表中的值:

SELECT 
    TL.ID, 
    TL.TableName, 
    TL.FileName, 
    TL.ContentID, 
    coalesce(p.PageName, C.CategoryName, PD.ProductName) AS Content
FROM
    ...

答案 2 :(得分:1)

尝试 // Context context; // You don't need to store the context here as you are in the activity // CardView cardview; // Don't need to be global // RelativeLayout.LayoutParams layoutParamsRL; // Don't need to be global ImageView imageView; RelativeLayout relativeLayout; static int pos = 0; // Are you sure it needs to be a static field? @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // context=this; relativeLayout = (RelativeLayout) findViewById(R.id.rl_layout); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { createCardViewWithoutImageView(); return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { return true; } public void createCardViewWithoutImageView() { //CardView CardView cardview = new CardView(MainActivity.this); cardview.setId(++pos); cardview.setLayoutParams((new CardView.LayoutParams(600, 600))); cardview.setContentPadding(25, 25, 25, 25); cardview.setCardBackgroundColor(Color.MAGENTA); cardview.setOnTouchListener(mListener); relativeLayout.addView(cardview); } private final View.OnTouchListener mListener = new View.OnTouchListener() { float dx, dy; // Moved outside the onTouch method in order to them to keep their value. @Override public boolean onTouch(View card, MotionEvent event) { Log.d("OnTouchListener", "don't touch me :p"); // The View (v) here is a CardView as the onTouchListener is set to it RelativeLayout.LayoutParams layoutParamsRL = (RelativeLayout.LayoutParams) ((CardView) card).getLayoutParams(); float x = 0, y = 0; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { card.bringToFront(); dx = event.getRawX() - layoutParamsRL.leftMargin; dy = event.getRawY() - layoutParamsRL.topMargin; } break; case MotionEvent.ACTION_MOVE: { x = event.getRawX(); y = event.getRawY(); layoutParamsRL.leftMargin = (int) (x - dx); layoutParamsRL.topMargin = (int) (y - dy); card.setLayoutParams(layoutParamsRL); } break; case MotionEvent.ACTION_UP: { } break; } return true; } }; 功能:

concat