PostgreSQL循环遍历子查询

时间:2015-12-28 21:49:31

标签: sql postgresql

我想遍历select子查询的结果并使用它们从另一个表中获取数据:

SELECT alias, profile_picture 
FROM public.profile 
where user_id = (
    SELECT friend_user_id 
    FROM public.friends 
    where user_id = 1059948007363671)::text

显然这个查询返回:

ERROR:  more than one row returned by a subquery used as an expression

循环子查询结果的最佳方法是什么,并将其设置为pubic.profile可用于获取相应的配置文件信息的临时变量???

2 个答案:

答案 0 :(得分:1)

<div ng-controller="Step1Ctrl" layout="column" ng-cloak="" class="autocompletedemoBasicUsage" ng-app="MyApp">
  <md-content class="md-padding">
    <form name="Form">

      <fieldset layout-gt-sm="row" layout-align="center" data-ng-repeat="child in childern">

        <md-input-container class="md-block">
          <label>First Name</label>
          <input required name="firstName" ng-model="child.firstName">
          <div ng-messages="Form.firstName.$error">
            <div ng-message="required">First Name is required.</div>
          </div>
        </md-input-container>
        <md-input-container class="md-block" style="max-width:60px;">
          <label>MI</label>
          <input required name=" middleName " ng-model="child.middleName ">
          <div ng-messages="Form.middleName.$error ">
            <div ng-message="required ">Middle Name is required.</div>
          </div>
        </md-input-container>
        <md-input-container class="md-block ">
          <label>Last Name</label>
          <input required name="lastName " ng-model="child.lastName ">
          <div ng-messages="Form.lastName.$error ">
            <div ng-message="required ">Last Name is required.</div>
          </div>
        </md-input-container>

        <div class="cell ">
          <label> <b>Student?</b>
          </label>
          <div class="box">
            <md-radio-group ng-model="child.IsStudent">
              <md-radio-button value="Yes">Yes</md-radio-button>
              <md-radio-button value="No">No</md-radio-button>
            </md-radio-group>
          </div>
        </div>

        <div class="cell">
          <label><b> Child's situation? </b>
          </label>
          <div class="box">
            <md-radio-group ng-model="child.situation">
              <md-radio-button value="Foster Child">Foster Child</md-radio-button>
              <md-radio-button value="HMR">Homeless, Migrant, Runaway</md-radio-button>
            </md-radio-group>
          </div>
        </div>
      </fieldset>

    </form>
    <div layout-gt-sm="row" layout-align="center">
      <md-button class="md-raised md-primary" ng-click="addChild()">Add Child</md-button>
    </div>
  </md-content>
</div>

答案 1 :(得分:0)

使用in代替=

WHERE user_id IN 
    (
    SELECT friend_user_id::text
    FROM public.friends 
    WHERE user_id = 1059948007363671
    )