如何在mysql中合并这些列,还是应该使用其他连接?

时间:2017-11-24 16:07:29

标签: mysql sql join

所以我要从数据库中获取指定的信息。我设法做了类似的事情,但我不知道如何将weapon, armor, etc合并到一个名为items的列中

SELECT char_name
     , obj_Id
     , items.item_id
     , weapon.name as weapon
     , armor.name as armor
     , etcitem.name as etc
  FROM characters 
  join items 
    on characters.obj_Id = items.owner_id
  LEFT 
  JOIN weapon 
    ON items.item_id = weapon.item_id
  LEFT 
  JOIN armor 
    ON items.item_id = armor.item_id
  LEFT 
  JOIN etcitem 
    ON items.item_id = etcitem.item_id;

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用fcntl

Option Explicit

Sub colRange()
    Dim ws As Worksheet
    Dim hostCellRange As Range, cfind As Range
    Dim lRow As Long, lCol As Long

    Set ws = Worksheets("Sheet1")

    With ws
        With .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft))
            Set cfind = .Find(What:="host", LookIn:=xlValues, lookat:=xlWhole)
            If Not cfind Is Nothing Then
                '~~> Find the column number
                lCol = cfind.Column
                '~~> Find the last row in that column
                lRow = ws.Range(Split(Cells(, lCol).Address, "$")(1) & ws.Rows.Count).End(xlUp).Row
                '~~> Create the range
                Set hostCellRange = .Range(cfind.Offset(1, 0), cfind.Offset(lRow - 1, 0))
                Debug.Print hostCellRange.Address
            End If
        End With
    End With
End Sub

coalesce()函数返回列表中的第一个非空表达式。

<强>语法

SELECT char_name, obj_Id, items.item_id, coalesce(weapon.name, armor.name, etcitem.name) as item_name . . .