在SQL中组合两列

时间:2016-08-19 09:05:14

标签: sql

我希望以这样的方式组合两列,以便在第一列下面添加第二列。 例如:

$(document).on("click", ".detach", function() {
  var $row = $(this).parents('.row');
  var detachvalue = $(this).closest('div').prev().find('input.detachval').val();

  $("<div title='Important Message'>Are you sure you want to detach?</div>").dialog({
    modal: true,
    buttons: {
      Ok: function() {
        $row.remove();
        console.log(detachvalue);
        $(this).dialog("close");
      }
    }
  });
});

结果:

<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<div class="row">
  <div class="form-group col-md-5">
    <input type="text" class="form-control col-md-4 detachval" placeholder="No number currently">
  </div>
  <div class="form-group col-md-3">
    <button type="submit" class="btn btn-primary form-control detach">Detach</button>
  </div>
</div>
<div class="row">
  <div class="form-group col-md-5">
    <input type="text" class="form-control col-md-4 detachval" placeholder="No number currently">
  </div>
  <div class="form-group col-md-3">
    <button type="submit" class="btn btn-primary form-control detach">Detach</button>
  </div>
</div>
<div class="row">
  <div class="form-group col-md-5">
    <input type="text" class="form-control col-md-4 detachval" placeholder="No number currently">
  </div>
  <div class="form-group col-md-3">
    <button type="submit" class="btn btn-primary form-control detach">Detach</button>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

2 个答案:

答案 0 :(得分:3)

执行UNION ALL将两列作为一个列:

select col1 from tablename
UNION ALL
select col2 from tablename

如果您在col2值之前绝对需要col1值,请将其包装在派生表中并添加ORDER BY

select col from
(
    select col1 as col, 1 as ob from tablename
    UNION ALL
    select col2, 2 as ob from tablename
) dt
order by ob

答案 1 :(得分:0)

SELECT CONCAT(col1, col2) AS col FROM table