Новости
Документация
Download
Webboard
Поиск
FAQ/ЧаВо
Обратная связь




MySQL.RU - Webboard



Вернуться
Выборка со связкой из двух таблиц (Алексей) 24/10/2009 - 13:45:53
      Re: Выборка со связкой из двух таблиц (bac) 24/10/2009 - 14:26:12
      Re: Уточнение Выборка со связкой из двух таблиц (bac) 24/10/2009 - 14:35:37
      Re: Уточнение Выборка со связкой из двух таблиц (Алексей) 26/10/2009 - 14:48:54
      Re: Выборка со связкой из двух таблиц (Вячеслав) 13/11/2009 - 16:22:16

> Original message text:
> From: Алексей - 24/10/2009 - 13:45:53
> Subject:Выборка со связкой из двух таблиц
> -----------------
> Таблицы:
> ТОВАРЫ- Goods
> ------------------
> Goods_ID | Title
> ------------------
> 101 | aaa
> 102 | bbb
> 103 | ccc
> ...
>
>
> Разделы товаров - Goods_Sections
> ----------------------
> Goods_ID | Section_ID
> ----------------------
> 100 | 1
> 101 | 2
> 102 | 1
> 102 | 2
> 103 | 3
> ...
>
>
> Для выборки товаров пренадлежащих любому из указанных разделов, я делаю так:
> SELECT DISTINCT Goods.Goods_ID
> FROM Goods, Goods_Sections
> WHERE (
> Goods.Goods_ID=Goods_Sections.Goods_ID
> AND (
> Goods_Sections.Section_ID = '1'
> OR
> Goods_Sections.Section_ID = '2'
> )
> )
> Происходит выборка товаров из разделов 1 и 2, т.е. товары ID: 101, 102, 103
>
> Теперь встала задача выбрать товары пренадлежащие зразу двум указанным разделам.
> Замена OR на AND не помогла:
> SELECT DISTINCT Goods.Goods_ID
> FROM Goods, Goods_Sections
> WHERE (
> Goods.Goods_ID=Goods_Sections.Goods_ID
> AND (
> Goods_Sections.Section_ID = '1'
> AND
> Goods_Sections.Section_ID = '2'
> )
> )
>
> Возращается всегда пустой результат.
>
> Подскажите, возможно ли сделать такую выборку одним запросом?
>


From: Вячеслав - 13/11/2009 - 16:22:16
Subject:Выборка со связкой из двух таблиц
-----------------
В результате выполнения скрипта формируется список, где на каждого клиента приходится по две одинаковые строчки. И я никак не могу понять, почему идет дублирование строк!
Скрипт:
<?

$db_name = "stanadonru_okt09";

$connection = @mysql_connect("localhost", "stanadonru_okt09", "strela")
or die("Couldn't connect.");

$db = @mysql_select_db($db_name, $connection)
or die("Couldn't select database.");


$sql = "SELECT DISTINCT o.orderID, o.order_time, o.customer_lastname, o.customer_firstname, o.shipping_type,
o.payment_type, o.order_amount, o.statusid, o.customer_email, o.shipping_country, o.shipping_state,
o.shipping_zip, o.shipping_city, o.shipping_address, o.shipping_lastname, o.shipping_firstname,
o.cust_phone, s.status_comment
FROM
SS_orders AS o, SS_order_status_changelog AS s
WHERE
o.orderID=s.orderID AND o.statusID=4 ORDER BY o.orderID";

$result = @mysql_query($sql,$connection)
or die("Couldn't execute query.: ".mysql_error());

while ($row = mysql_fetch_array($result)) {
$orderID=$row['orderID'];
$order_time=$row['order_time'];
$customer_lastname = $row['customer_lastname'];
$customer_firstname = $row['customer_firstname'];
$shipping_type = $row['shipping_type'];
$payment_type = $row['payment_type'];
$order_amount = $row['order_amount'];
$statusid = $row['statusID'];
$customer_email = $row['customer_email'];
$shipping_country = $row['shipping_country'];
$shipping_state = $row['shipping_state'];
$shipping_zip = $row['shipping_zip'];
$shipping_city = $row['shipping_city'];
$shipping_address = $row['shipping_address'];
$shipping_lastname = $row['shipping_lastname'];
$shipping_firstname = $row['shipping_firstname'];
$cust_phone = $row['cust_phone'];
$status_comment = $row['status_comment'];

$display_block .= "
<P>Заказ №: $orderID<br>
Покупатель: $customer_firstname $customer_lastname<br>
Время заказа: $order_time<br>
Тип оплаты: $payment_type<br>
Тип доставки: $shipping_type<br>
Стоимость заказа: $order_amount<br>
Статус: $statusid <br>
E-mail: $customer_email<br>
Комментарии: $status_comment<br>
Адрес доставки: $shipping_address $shipping_city $shipping_state $shipping_country $shipping_zip<br>
Получатель посылки: $shipping_lastname $shipping_firstname<br>
Телефон: $cust_phone</P>";


}

?>
<html>
<body>
<? echo "$display_block"; ?>
</body>
</html>


[Это сообщение - спам!]

Последние сообщения из форума

Уважаемые посетители форума MySQL.RU!
Убедительная просьба, прежде чем задавать свой вопрос в этом форуме, обратите внимание на разделы:
- ответы на наиболее часто задаваемые вопросы - FAQ
- раздел документация
- раздел поиск по сообщениям форума и документации
Также, старайтесь наиболее подробно указывать свою ситуацию (версию операционной системы, версию MySQL, версию программного обеспечения, по которому возникает вопрос, текст возникающих ошибок, и др.)
Помните, чем конкретнее Вы опишете ситуацию, тем больше шансов получить реальную помощь.
 Имя:
 E-mail:
 Тема:
 Текст:
Код подтверждения отправки: Code
36445



РЕКЛАМА НА САЙТЕ
  Создание сайтов | |