PHP MySQL - INNER JOIN:userid =值的一部分

时间:2017-01-12 20:08:53

标签: php mysql

假设我得到了这张表:

用户:

userid       |      stuff
 777         |      bla
 888         |      bla2
 34          |      bla3
 66          |      bla4

用户:

user_visitor |
 777_34      |
 888_34      |
 777_66      |

我需要什么:

SELECT
    *
    FROM
    visitors INNER JOIN user
    ON
    (the part after _ from user_visitor) = userid
    WHERE
    user_visitor LIKE '%777_%'

因此,当我解析useridstuff时,我得到34 / bla366 / bla4

我怎么能这样做?它甚至可能吗?

1 个答案:

答案 0 :(得分:0)

这就是你想要做的,

$scope.gridOptions = {
    onRegisterApi: function(gridApi) {
      $scope.gridApi = gridApi;
      gridApi.core.on.rowsRendered($scope,function(){
        if(!$scope.initialColumnOrder)
          $scope.initialColumnOrder = $scope.gridApi.grid.columns.slice();
      });
    }
  };

$scope.reset = function(){
  if($scope.initialColumnOrder) {
    var columnDefsColMov = $scope.gridApi.grid.moveColumns.orderCache;
    columnDefsColMov.length = 0;
    columnDefsColMov.push.apply(columnDefsColMov, $scope.initialColumnOrder)
    $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
  }
}

但我建议您将user_visitor拆分为两个列,如userid和visitorid,然后您可以像这样添加唯一索引

scope :time_sorted, -> { 
    select("events.*, CASE 
      WHEN start_at <= '#{Time.now.utc}' AND end_at > '#{Time.now.utc}' THEN 1
      WHEN end_at < '#{Time.now.utc}' THEN 2
      WHEN start_at > '#{Time.now.utc}' AND start_at < '#{Time.now.utc+6.hours}' THEN 3 ELSE 4
      END AS formula")
    .order("formula ASC")
}

然后你的查询就像这样。

SELECT l1.stuff
FROM user_1 AS l1
INNER JOIN visitors AS l2
ON l2.user_visitor LIKE concat('%',l1.userid);

效率更高。 编辑:您可以在phpmyadmin中执行这些操作。