如何为另一个查询的每一行使用变量运行查询

时间:2019-05-28 11:39:48

标签: mysql

我有一个桌子工作室,每个工作室可以有一个parentStudio。

<form action="/scripts/contact-form.php" enctype="multipart/form-data" method="post" name="contactform" id="contactform" data-validate>
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter Name">
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter Email" required>
<label for="group_size">Group Size (minimum of 6):</label>
<input type="number" id="group_size" name="group_size" min="6" max="999" placeholder="Enter Approx Group Size">
<label for="message">Message:</label>
<textarea form="contactform" id="message" name="message" placeholder="Enter Message" required onblur=""></textarea>
<button type="submit">Send</button>
</form> 


    <script src="https://static.codepen.io/assets/common/stopExecutionOnTimeout-de7e2ef6bfefd24b79a3f68b414b87b8db5b08439cac3f1012092b2290c719cd.js"></script>
    <script src='https://cdn.jsdelivr.net/gh/cferdinandi/bouncer@1/dist/bouncer.polyfills.min.js'></script>
    <script id="rendered-js">
          var bouncer = new Bouncer('[data-validate]', {
      disableSubmit: true,
      customValidations: {
        valueMismatch: function (field) {

          // Look for a selector for a field to compare
          // If there isn't one, return false (no error)
          var selector = field.getAttribute('data-bouncer-match');
          if (!selector) return false;

          // Get the field to compare
          var otherField = field.form.querySelector(selector);
          if (!otherField) return false;

          // Compare the two field values
          // We use a negative comparison here because if they do match, the field validates
          // We want to return true for failures, which can be confusing
          return otherField.value !== field.value;

        } },

      messages: {
        valueMismatch: function (field) {
          var customMessage = field.getAttribute('data-bouncer-mismatch-message');
      return customMessage ? customMessage : 'Please make sure the fields match.';
    } } });



document.addEventListener('bouncerFormInvalid', function (event) {
  console.log(event.detail.errors);
  console.log(event.detail.errors[0].offsetTop);
  window.scrollTo(0, event.detail.errors[0].offsetTop);
}, false);

document.addEventListener('bouncerFormValid', function () {
  alert('Form submitted successfully!');
  window.location.removedByCodePen();
}, false);
      //# sourceURL=pen.js
    </script>
<script src="https://static.codepen.io/assets/editor/live/css_reload-5619dc0905a68b2e6298901de54f73cefe4e079f65a75406858d92924b4938bf.js"></script>

我需要查询符合某些条件的工作室,但是我还需要让所有找到的工作室的所有父母都拥有。我查询了符合条件的工作室,也查询了所有已知工作室的父母。我不知道如何将它们组合成一个查询。

我用来获取所有符合条件的工作室的查询基本上是这样的:

+----+------+--------------+----------+
| id | name | parentStudio | criteria |
+----+------+--------------+----------+
| 0  | A    | 6            | 0        |
+----+------+--------------+----------+
| 1  | B    | 0            | 1        |
+----+------+--------------+----------+
| 2  | C    | null         | 0        |
+----+------+--------------+----------+
| 3  | D    | 2            | 1        |
+----+------+--------------+----------+
| 4  | E    | null         | 1        |
+----+------+--------------+----------+
| 5  | F    | null         | 0        |
+----+------+--------------+----------+
| 6  | G    | null         | 0        |
+----+------+--------------+----------+

我用来获取所有父母的查询是:

SELECT id FROM Studios WHERE criteria = 1
+----+
| id |
+----+
| 1  |
+----+
| 3  |
+----+
| 4  |
+----+

我要寻找的结果看起来很天真:

SELECT
    @currentId AS _id,
    (
        SELECT
            name
        FROM Studios
        WHERE id = _id
    ) AS name,
    (
        SELECT
            @currentId := parentStudio
        FROM Studios
        WHERE id = _id
    ) AS parentStudio,
    @level := @level + 1 AS level
FROM
    (
        SELECT
            @currentId := 1, --starting point
            @level := 0
    ) AS vars,
    Studios AS s
WHERE
    @currentId IS NOT NULL
+----+------+--------------+-------+
| id | name | parentStudio | level |
+----+------+--------------+-------+
| 1  | B    | 0            | 0     |
+----+------+--------------+-------+
| 0  | A    | 6            | 1     |
+----+------+--------------+-------+
| 6  | G    | null         | 2     |
+----+------+--------------+-------+

但是显然,这是行不通的,因为SELECT DISTINCT _id AS id, name, parentStudio, level FROM (SELECT @currentId AS _id, ( SELECT name FROM Studios WHERE id = _id ) AS name, ( SELECT @currentId := parentStudio FROM Studios WHERE id = _id ) AS parentStudio, @level := @level + 1 AS level FROM ( SELECT @currentId := (SELECT id FROM Studios WHERE criteria = 1), @level := 0 ) AS vars, Studios AS s WHERE @currentId IS NOT NULL ) 提供了多个结果。

所需的结果集如下所示:

(SELECT id FROM Studios WHERE criteria = 1)

请注意,结果集中没有ID 5(名称F)。 它本身不符合条件,也不是符合条件的任何工作室的祖先。

有什么方法可以仅在SQL(MySQL 5)中完成吗?

0 个答案:

没有答案