<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="assignment-operators">
      </a>
      14.4.4 Assignment Operators
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045206875952">
  </a>
  <a class="indexterm" name="idm46045206874880">
  </a>
  <div class="table">
   <a name="idm46045206873392">
   </a>
   <p class="title">
    <b>
     Table 14.6 Assignment Operators
    </b>
   </p>
   <div class="table-contents">
    <table frame="box" rules="all" summary="A reference that lists assignment operators.">
     <colgroup>
      <col style="width: 28%"/>
      <col style="width: 71%"/>
     </colgroup>
     <thead>
      <tr>
       <th>
        Name
       </th>
       <th>
        Description
       </th>
      </tr>
     </thead>
     <tbody>
      <tr>
       <td>
        <a class="link" href="assignment-operators.html#operator_assign-value">
         <code class="literal">
          :=
         </code>
        </a>
       </td>
       <td>
        Assign a value
       </td>
      </tr>
      <tr>
       <td>
        <a class="link" href="assignment-operators.html#operator_assign-equal">
         <code class="literal">
          =
         </code>
        </a>
       </td>
       <td>
        Assign a value (as part of a
        <a class="link" href="set-variable.html" title="15.7.6.1 SET Syntax for Variable Assignment">
         <code class="literal">
          SET
         </code>
        </a>
        statement, or as part of the
        <code class="literal">
         SET
        </code>
        clause in an
        <a class="link" href="update.html" title="15.2.17 UPDATE Statement">
         <code class="literal">
          UPDATE
         </code>
        </a>
        statement)
       </td>
      </tr>
     </tbody>
    </table>
   </div>
  </div>
  <br class="table-break"/>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <a name="operator_assign-value">
     </a>
     <p>
      <a class="link" href="assignment-operators.html#operator_assign-value">
       <code class="literal">
        :=
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206853888">
     </a>
     <a class="indexterm" name="idm46045206852800">
     </a>
     <p>
      Assignment operator. Causes the user variable on the left
            hand side of the operator to take on the value to its right.
            The value on the right hand side may be a literal value,
            another variable storing a value, or any legal expression
            that yields a scalar value, including the result of a query
            (provided that this value is a scalar value). You can
            perform multiple assignments in the same
      <a class="link" href="set-variable.html" title="15.7.6.1 SET Syntax for Variable Assignment">
       <code class="literal">
        SET
       </code>
      </a>
      statement. You can perform multiple assignments in the same
            statement.
     </p>
     <p>
      Unlike
      <a class="link" href="assignment-operators.html#operator_assign-equal">
       <code class="literal">
        =
       </code>
      </a>
      , the
      <a class="link" href="assignment-operators.html#operator_assign-value">
       <code class="literal">
        :=
       </code>
      </a>
      operator is never interpreted as a comparison operator. This
            means you can use
      <a class="link" href="assignment-operators.html#operator_assign-value">
       <code class="literal">
        :=
       </code>
      </a>
      in
            any valid SQL statement (not just in
      <a class="link" href="set-variable.html" title="15.7.6.1 SET Syntax for Variable Assignment">
       <code class="literal">
        SET
       </code>
      </a>
      statements) to assign a value to a variable.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa57347645"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span> <span class="token operator">:=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span> <span class="token operator">:=</span> <span class="token variable">@var1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token operator">:=</span><span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token keyword">FROM</span> t1<span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">4</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">4</span></code></pre>
     </div>
     <p>
      You can make value assignments using
      <a class="link" href="assignment-operators.html#operator_assign-value">
       <code class="literal">
        :=
       </code>
      </a>
      in
            other statements besides
      <a class="link" href="select.html" title="15.2.13 SELECT Statement">
       <code class="literal">
        SELECT
       </code>
      </a>
      , such as
      <a class="link" href="update.html" title="15.2.17 UPDATE Statement">
       <code class="literal">
        UPDATE
       </code>
      </a>
      , as shown here:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa91733653"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">4</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t1<span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">UPDATE</span> t1 <span class="token keyword">SET</span> c1 <span class="token operator">=</span> <span class="token number">2</span> <span class="token keyword">WHERE</span> c1 <span class="token operator">=</span> <span class="token variable">@var1</span><span class="token operator">:=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token output">Query OK, 1 row affected (0.00 sec)</span>
<span class="token output">Rows matched: 1  Changed: 1  Warnings: 0</span>

<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t1<span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span></code></pre>
     </div>
     <p>
      While it is also possible both to set and to read the value
            of the same variable in a single SQL statement using the
      <a class="link" href="assignment-operators.html#operator_assign-value">
       <code class="literal">
        :=
       </code>
      </a>
      operator, this is not recommended.
      <a class="xref" href="user-variables.html" title="11.4 User-Defined Variables">
       Section 11.4, “User-Defined Variables”
      </a>
      , explains why you should
            avoid doing this.
     </p>
    </li>
    <li class="listitem">
     <a name="operator_assign-equal">
     </a>
     <p>
      <a class="link" href="assignment-operators.html#operator_assign-equal">
       <code class="literal">
        =
       </code>
      </a>
     </p>
     <a class="indexterm" name="idm46045206819472">
     </a>
     <a class="indexterm" name="idm46045206818400">
     </a>
     <a class="indexterm" name="idm46045206816944">
     </a>
     <p>
      This operator is used to perform value assignments in two
            cases, described in the next two paragraphs.
     </p>
     <p>
      Within a
      <a class="link" href="set-variable.html" title="15.7.6.1 SET Syntax for Variable Assignment">
       <code class="literal">
        SET
       </code>
      </a>
      statement,
      <code class="literal">
       =
      </code>
      is treated as an assignment
            operator that causes the user variable on the left hand side
            of the operator to take on the value to its right. (In other
            words, when used in a
      <a class="link" href="set-variable.html" title="15.7.6.1 SET Syntax for Variable Assignment">
       <code class="literal">
        SET
       </code>
      </a>
      statement,
      <code class="literal">
       =
      </code>
      is treated identically to
      <a class="link" href="assignment-operators.html#operator_assign-value">
       <code class="literal">
        :=
       </code>
      </a>
      .)
            The value on the right hand side may be a literal value,
            another variable storing a value, or any legal expression
            that yields a scalar value, including the result of a query
            (provided that this value is a scalar value). You can
            perform multiple assignments in the same
      <a class="link" href="set-variable.html" title="15.7.6.1 SET Syntax for Variable Assignment">
       <code class="literal">
        SET
       </code>
      </a>
      statement.
     </p>
     <p>
      In the
      <code class="literal">
       SET
      </code>
      clause of an
      <a class="link" href="update.html" title="15.2.17 UPDATE Statement">
       <code class="literal">
        UPDATE
       </code>
      </a>
      statement,
      <code class="literal">
       =
      </code>
      also acts as an assignment operator; in
            this case, however, it causes the column named on the left
            hand side of the operator to assume the value given to the
            right, provided any
      <code class="literal">
       WHERE
      </code>
      conditions that
            are part of the
      <a class="link" href="update.html" title="15.2.17 UPDATE Statement">
       <code class="literal">
        UPDATE
       </code>
      </a>
      are
            met. You can make multiple assignments in the same
      <code class="literal">
       SET
      </code>
      clause of an
      <a class="link" href="update.html" title="15.2.17 UPDATE Statement">
       <code class="literal">
        UPDATE
       </code>
      </a>
      statement.
     </p>
     <p>
      In any other context,
      <code class="literal">
       =
      </code>
      is treated as a
      <a class="link" href="comparison-operators.html#operator_equal">
       comparison operator
      </a>
      .
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa77973685"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span> <span class="token operator">:=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token boolean">NULL</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span> <span class="token operator">:=</span> <span class="token variable">@var1</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token variable">@var1</span><span class="token punctuation">,</span> <span class="token variable">@var2</span><span class="token punctuation">;</span>
<span class="token prompt">        -&gt;</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span></code></pre>
     </div>
     <p>
      For more information, see
      <a class="xref" href="set-variable.html" title="15.7.6.1 SET Syntax for Variable Assignment">
       Section 15.7.6.1, “SET Syntax for Variable Assignment”
      </a>
      ,
      <a class="xref" href="update.html" title="15.2.17 UPDATE Statement">
       Section 15.2.17, “UPDATE Statement”
      </a>
      , and
      <a class="xref" href="subqueries.html" title="15.2.15 Subqueries">
       Section 15.2.15, “Subqueries”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
