<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3893036</guid>
        <pubDate>Tue, 18 Jul 2023 18:23:08 +0000</pubDate>
        <title>Необходима помощь с составлением SQL-запроса</title>
        <link>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3893036</link>
        <description><![CDATA[Akina: Если гуглить - то по термину &quot;логическое деление&quot;..]]></description>
        <author>Akina</author>
        <category>Базы данных: SQL</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3893026</guid>
        <pubDate>Tue, 18 Jul 2023 14:06:07 +0000</pubDate>
        <title>Необходима помощь с составлением SQL-запроса</title>
        <link>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3893026</link>
        <description><![CDATA[Костик+: Простите за отложенный ответ. <strong class='tag-b'>Majestio</strong>, <strong class='tag-b'>Akina</strong>, спасибо вам, что уделили время и помогли мне.<br>
Вот примерно такой синтаксис я и искал, в своей практике не встреч. Но, загуглив, нашёл ключевые слова для поиска &quot;WHERE IS multicolumn&quot;. Буду читать, разбираться.<br>
Попробовал на MSSQL - ругается, мол такой синтаксис не понимает. Попробую ещё на MySQL/MariaDB для сравнения, проверю и отпишу чуть позже.<br>
<br>
Ещё раз, всем - спасибо&#33;]]></description>
        <author>Костик+</author>
        <category>Базы данных: SQL</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892856</guid>
        <pubDate>Wed, 12 Jul 2023 19:57:21 +0000</pubDate>
        <title>Необходима помощь с составлением SQL-запроса</title>
        <link>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892856</link>
        <description><![CDATA[Majestio: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=436400&view=findpost&p=3892855'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Akina &#064; <time class="tag-quote__quoted-time" datetime="2023-07-12T17:46:02+00:00">12.07.23, 17:46</time></span><div class='quote '>Не, фигня получится - на показанных данных такой запрос вернёт ещё и ProductID=4, у которого AttrId = 8 имеет значение AttrValueId = 331.</div></div><br>
Согласен. Я невнимательно прочел условие. Тогда альтернативный вариант запроса:<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">SELECT </div><div class="code_line">&nbsp;&nbsp;A.ProductId</div><div class="code_line">FROM </div><div class="code_line">&nbsp;&nbsp;ProductTable AS A</div><div class="code_line">LEFT JOIN </div><div class="code_line">&nbsp;&nbsp;ProductTable AS B </div><div class="code_line">&nbsp;&nbsp; &nbsp;ON A.ProductId = B.ProductId</div><div class="code_line">WHERE </div><div class="code_line">&nbsp;&nbsp;(A.AttrId = 5 AND A.AttrValueId = 1010) AND (B.AttrId = 8 AND B.AttrValueId = 352)</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script><br>
Какой из запросов будет лучше - я без понятия.]]></description>
        <author>Majestio</author>
        <category>Базы данных: SQL</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892855</guid>
        <pubDate>Wed, 12 Jul 2023 17:46:02 +0000</pubDate>
        <title>Необходима помощь с составлением SQL-запроса</title>
        <link>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892855</link>
        <description><![CDATA[Akina: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=436400&view=findpost&p=3892853'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Majestio &#064; <time class="tag-quote__quoted-time" datetime="2023-07-12T17:17:25+00:00">12.07.23, 17:17</time></span><div class='quote '>В операторе WHERE собирай условия не по &quot;и&quot;, а по &quot;или&quot;, и всё получится</div></div><br>
Не, фигня получится - на показанных данных такой запрос вернёт ещё и ProductID=4, у которого AttrId = 8 имеет значение AttrValueId = 331.]]></description>
        <author>Akina</author>
        <category>Базы данных: SQL</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892853</guid>
        <pubDate>Wed, 12 Jul 2023 17:17:25 +0000</pubDate>
        <title>Необходима помощь с составлением SQL-запроса</title>
        <link>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892853</link>
        <description><![CDATA[Majestio: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=436400&view=findpost&p=3892840'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Костик&#043; &#064; <time class="tag-quote__quoted-time" datetime="2023-07-12T13:32:40+00:00">12.07.23, 13:32</time></span><div class='quote '>Т.е., получается что-то типа такого:</div></div><br>
<s class='tag-s'>В операторе WHERE собирай условия не по &quot;и&quot;, а по &quot;или&quot;, и всё получится:<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">SELECT</div><div class="code_line">&nbsp;&nbsp;ProductID</div><div class="code_line">FROM</div><div class="code_line">&nbsp;&nbsp;ProductTable</div><div class="code_line">WHERE</div><div class="code_line">&nbsp;&nbsp;(AttrId = 5 AND AttrValueId = 1010)</div><div class="code_line">&nbsp;&nbsp;OR -- &#60;--- тут</div><div class="code_line">&nbsp;&nbsp;(AttrId = 8 AND AttrValueId = 352)</div></ol></div></div></div></div></s>]]></description>
        <author>Majestio</author>
        <category>Базы данных: SQL</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892851</guid>
        <pubDate>Wed, 12 Jul 2023 16:23:29 +0000</pubDate>
        <title>Необходима помощь с составлением SQL-запроса</title>
        <link>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892851</link>
        <description><![CDATA[Костик+: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=436400&view=findpost&p=3892842'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Majestio &#064; <time class="tag-quote__quoted-time" datetime="2023-07-12T13:51:32+00:00">12.07.23, 13:51</time></span><div class='quote '>В таблице ProductId = 1 имеет пять AttrId ... дальше не читал</div></div><br>
Да, спасибо. Резонное замечание. Прошу прощения, с утра не выспался. Имеется ввиду, что параметр AttrId уникален для каждого ProductId. Т.е. ситуация:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">ProductId | AttrId</div><div class="code_line">----------+--------</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; |5</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; |6</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; |7</div><div class="code_line">//...</div></ol></div></div></div></div><br>
может быть, а: <br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">ProductId | AttrId</div><div class="code_line">----------+--------</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; |4</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; |4</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; |4</div></ol></div></div></div></div><br>
не может.]]></description>
        <author>Костик+</author>
        <category>Базы данных: SQL</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892850</guid>
        <pubDate>Wed, 12 Jul 2023 16:20:57 +0000</pubDate>
        <title>Необходима помощь с составлением SQL-запроса</title>
        <link>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892850</link>
        <description><![CDATA[Akina: <div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">SELECT ProductID</div><div class="code_line">FROM ProductTable</div><div class="code_line">WHERE ( AttrId, AttrValueId ) IN ( (5, 1010), (8, 352) )</div><div class="code_line">GROUP BY ProductID</div><div class="code_line">HAVING COUNT( /* DISTINCT */ AttrId ) = 2</div></ol></div></div></div></div>]]></description>
        <author>Akina</author>
        <category>Базы данных: SQL</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892842</guid>
        <pubDate>Wed, 12 Jul 2023 13:51:32 +0000</pubDate>
        <title>Необходима помощь с составлением SQL-запроса</title>
        <link>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892842</link>
        <description><![CDATA[Majestio: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=436400&view=findpost&p=3892840'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Костик&#043; &#064; <time class="tag-quote__quoted-time" datetime="2023-07-12T13:32:40+00:00">12.07.23, 13:32</time></span><div class='quote '>Каждый ProductId может иметь только один AttrId.</div></div><br>
В таблице ProductId = 1 имеет пять AttrId ... дальше не читал :)]]></description>
        <author>Majestio</author>
        <category>Базы данных: SQL</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892840</guid>
        <pubDate>Wed, 12 Jul 2023 13:32:40 +0000</pubDate>
        <title>Необходима помощь с составлением SQL-запроса</title>
        <link>https://forum.sources.ru/index.php?showtopic=436400&amp;view=findpost&amp;p=3892840</link>
        <description><![CDATA[Костик+: Доброго времени суток, всем&#33;<br>
Собственно, сабж.<br>
<br>
Есть таблица вида:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">ProductId &nbsp; |AttrId &nbsp; &nbsp; &nbsp; &nbsp;|AttrValueId &nbsp;</div><div class="code_line">------------+--------------+-----------------</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |1010</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |7 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |954</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |352</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |21057</div><div class="code_line">1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |14 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|221</div><div class="code_line">2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |997</div><div class="code_line">2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |2350</div><div class="code_line">2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |352</div><div class="code_line">2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |20533</div><div class="code_line">2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |14 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|232</div><div class="code_line">3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |1010</div><div class="code_line">3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |352</div><div class="code_line">3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |27044</div><div class="code_line">4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |1010</div><div class="code_line">4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |7 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |964</div><div class="code_line">4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |331</div><div class="code_line">4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |14 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|219</div><div class="code_line">//...</div></ol></div></div></div></div><br>
<br>
Каждый ProductId может иметь только один уникальный AttrId.<br>
Необходимо выбрать из таблицы все ProductId, удовлетворяющие условию, когда определённые атрибуты содержат соответствующие Value. Например,<br>
выбрать товары, в для которых AttrId 5 содержит Value 1010 и AttrId 8 содержит Value 352:<br>
5 =&gt; 1010 и 8 =&gt; 352<br>
<br>
Т.е., получается что-то типа такого:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">SELECT </div><div class="code_line">&nbsp;&nbsp;ProductID </div><div class="code_line">FROM </div><div class="code_line">&nbsp;&nbsp;ProductTable</div><div class="code_line">WHERE</div><div class="code_line">&nbsp;&nbsp;(AttrId = 5 AND AttrValueId = 1010)</div><div class="code_line">&nbsp;&nbsp;AND</div><div class="code_line">&nbsp;&nbsp;(AttrId = 8 AND AttrValueId = 352)</div></ol></div></div></div></div><br>
<br>
В результате выполнения подобного запроса для таблицы из примера выше, получим выборку:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">ProductId</div><div class="code_line">----------</div><div class="code_line">1</div><div class="code_line">3</div></ol></div></div></div></div><br>
<br>
ProductId 2 и 4 не подойдут, т.к. для них выполняется только одно из условий.<br>
<br>
БД: MSSQL.<br>
<br>
Надеюсь, правильно сформулировал свою мысль.]]></description>
        <author>Костик+</author>
        <category>Базы данных: SQL</category>
      </item>
	
      </channel>
      </rss>
	