Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
  • Sign Up

Помощь в создании запроса sql


Recommended Posts

Сделал такой список:
 
a3d07afe8d8f.png
 
Не получается сделать запрос к другой таблице(notify_when_arrives) чтобы подсчитать количество ожидающих клиентов
 
В той таблице есть колонка с id товаров - они повторяются (к примеру 5 раз 63, 2 раза 66 и тд)
 
Почему то колонку "Ожидают" считает не правильно, хотя делал по аналогии с колонкой "Куплено"

 

language_id в таблице notify_when_arrives - всегда 1

Запрос к таблицам

    public function getprr($data = array()) {

        $sql  = "SELECT p.product_id, ";
        $sql .= "pd.name AS product_name, ";
        $sql .= "SUM(op.quantity) AS purchased, "; // подсчет купленных товаров
        $sql .= "SUM(nwa.language_id) AS expecting, "; // подсчет ожиданий
        $sql .= "p.viewed, ";
        $sql .= "p.quantity AS product_quantity, ";
        $sql .= "p.orderednoopt ";
	$sql .= "FROM ".DB_PREFIX."product p ";
        $sql .= "LEFT JOIN ".DB_PREFIX."product_description pd ON pd.product_id=p.product_id ";
        $sql .= "LEFT JOIN ".DB_PREFIX."order_product op ON op.product_id=p.product_id ";
        $sql .= "LEFT JOIN ".DB_PREFIX."notify_when_arrives nwa ON nwa.product_id=p.product_id ";
        
        $sql .= "WHERE p.status = '1' ";
        
        $sort_data = array(
    		'p.product_id',
                'pd.name',
                'purchased',
                'p.viewed',
                'expecting'
                'p.quantity',
                'p.orderednoopt',
			);
            
            $sql .= " GROUP BY p.product_id";
            
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			} else {
				$sql .= " ORDER BY pd.name";
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
        
        $query = $this->db->query($sql);
        return $query->rows;
    }


Код из php

$prrs = $this->model_report_roptions->getprr($data);
    	
foreach ($prrs as $prr) {
	$this->data['prods'][] = array(
                'product_id'        => $prr['product_id'],
                'product_name'      => $prr['product_name'],
                'purchased'         => $prr['purchased'],
                'views'             => $prr['viewed'],
                'expecting'         => $prr['expecting'],
                'product_quantity'  => $prr['product_quantity'],
                'orderednoopt'      => $prr['orderednoopt']
			);
		}


Код из tpl

      <tbody>
          <?php foreach ($prods as $prod) { ?>
          <tr>
            <td><?php echo $prod['product_id']; ?></td>
            <td><?php echo $prod['product_name']; ?></td>
            <td><?php if (isset($prod['purchased'])) { echo $prod['purchased']; } else { echo 0; } ?></td>
            <td><?php echo $prod['views']; ?></td>
            <td><?php echo $prod['expecting']; ?></td>
            <td><?php echo $prod['product_quantity']; ?></td>
            <td><?php echo $prod['orderednoopt']; ?></td>
          </tr>
          <?php } ?>
        </tbody>

 

Подскажите пожалуйста, что не правильно делаю?

Link to post
Share on other sites

По идее запрос надо такой:

SELECT p.product_id,

(SELECT COUNT(*) FROM notify_when_arrives nwa WHERE nwa.product_id=p.product_id) AS arrives_count,
pd.name AS product_name,

... Дальше всё остальное

 

LEFT JOIN notify_when_arrives из запроса надо убрать.

  • +1 1
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.