Jump to content

[SOLVED] Help with QUERY


Canman2005

Recommended Posts

Hi all

 

I really need a hand, im getting into slightly complext QUERIES and really could do with some help.

 

Basically I have the following QUERY

 

SELECT p.id, p.image_1, p.title, p.description, p.price, p.live , p.category, l.product_id, l.allergy_id FROM products p LEFT JOIN allergies_products l ON (p.id = l.product_id) WHERE p.live = 1 AND p.category = '15' AND l.allergy_id !=17 GROUP BY l.product_id ORDER BY p.title ASC

 

This gets all products from a main `products` table, but ignores any rows which are linked in the `allergies_products` table which have a `allergy_id` of 17 (or whatever the variable is)

 

inside the table `allergies_products` I store;

 

id

product_id

dietary_id

 

although my QUERY is looking anything without `l.allergy_id !=17` it could be that the same product ID is listed twice in the `allergies_products` table but with different `allergy_id` values.

 

How can I get the QUERY to ignore any rows with a `allergy_id` value of 17 (like it currently does), but then also ignore all rows that have the same `product_id` value as the rows don't have an `allergy_id` value of 17.

 

Does that make any sense?

 

Please can some help me? You're my only hope

 

Thanks in advance :'(

Link to comment
Share on other sites

Got any data we can test with?

 

for now

SELECT p.id, p.image_1, p.title, p.description, p.price, 
    p.live , p.category, l.product_id, l.allergy_id 
FROM products p 
    LEFT JOIN allergies_products l ON (p.id = l.product_id) AND l.allergy_id != 17
WHERE p.live = 1 AND p.category = '15'          
GROUP BY l.product_id 
ORDER BY p.title ASC

Link to comment
Share on other sites

Thanks, but that seemed to return incorrect too.

 

Here is some sample data

 

-----------------The Allergies Products Table ----------------------------

 

CREATE TABLE IF NOT EXISTS `allergies_products` (

  `id` int(4) NOT NULL auto_increment,

  `product_id` int(10) NOT NULL,

  `allergy_id` int(10) NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

 

INSERT INTO `allergies_products` (`id`, `product_id`, `allergy_id`) VALUES

(5, 1, 13),

(4, 1, 28),

(6, 1, 31),

(7, 2, 30),

(8, 3, 31);

 

-----------------The Products Table ----------------------------

 

CREATE TABLE IF NOT EXISTS `products` (

  `id` int(4) NOT NULL auto_increment,

  `category` int(4) NOT NULL default '0',

  `title` varchar(255) NOT NULL default '',

  `image_1` int(4) default NULL,

  `description` text,

  `live` int(1) NOT NULL default '0',

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

 

--

-- Dumping data for table `products`

--

 

INSERT INTO `products` (`id`, `image_1`, `title`, `description`, `price`, `live`, `category`) VALUES

(1, 15, 'Burger', '', 2.99, 1, 1),

(2, 12, 'HotDog', '', 2.99, 1, 1),

(3, 15, 'Sausage Rolls', '', 2.99, 1, 1);

 

 

 

So as you can see, product ID 1 is listed 3 times in the `allergies_products` table, so with my QUERY, if you selected NOT to rerturn any rows with the value 31 in the `allergy_id` field, then the results would only be

 

(7, 2, 30)

 

because `allergy_id` 31 is lined to `product_id` 1 and 3, therefore it shouldnt return any rows which have ID 1 or 3 in them.

 

Does that make any kind of sense?

 

Thanks very much

Link to comment
Share on other sites

any closer?

SELECT p.id, p.image_1, p.title, p.description, p.price,
    p.live , p.category, l.product_id, l.allergy_id
FROM products p
  LEFT JOIN allergies_products l ON l.product_id = p.id
WHERE p.live = 1
AND p.category = '1'
AND p.id NOT IN (
  SELECT product_id FROM allergies_products
  WHERE allergy_id = '31'
)
ORDER BY p.title ASC

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.