Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8707045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:46:04+00:00 2026-06-13T03:46:04+00:00

I have three tables in a database: Product table – +100000 entries Attribute table

  • 0

I have three tables in a database:

  • Product table – +100000 entries
  • Attribute table (list of possible attributes of a product)
  • Product attribtue table (which contains the value of the attribute of a product)

I am looking for 8 random products and one of their attributes (attribute_id = 2), but if a product hasn’t this attribute it should appear at the return of the query. I have been trying some sql queries without any succesful result because my return only shows the products that have the attribute and hide the others.

My three tables are like this:

CREATE TABLE `product` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `sku` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `name` varchar(90) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `provider_id` int(11) unsigned DEFAULT NULL,
  `url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `active` int(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `sku_UNIQUE` (`sku`)
) ENGINE=InnoDB AUTO_INCREMENT=123965 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `attribute` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL DEFAULT '',
  `data_type` varchar(50) DEFAULT '',
  PRIMARY KEY (`id`), 
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1;

CREATE TABLE `product_attribute` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `product_id` int(11) unsigned NOT NULL,
  `attribute_id` int(11) unsigned NOT NULL DEFAULT '6',
 `value` longtext NOT NULL,
 PRIMARY KEY (`id`),
  UNIQUE KEY `product_id` (`product_id`,`attribute_id`)
) ENGINE=InnoDB AUTO_INCREMENT=869437 DEFAULT CHARSET=latin1;

And this is one of the queries I tried, I thought it was correct but it have the same problem as the others…

SELECT product.id, product.sku, product.name,provider.name as provider_name,
product_attribute.value as author 
FROM (`product`)  
LEFT JOIN `provider` ON `product`.`provider_id` = `provider`.`id` 
LEFT JOIN `product_attribute` ON `product`.`id` = `product_attribute`.`product_id`
 WHERE `product`.`active` = '1' AND `product`.`url` IS NOT NULL 
AND (`product_attribute`.`attribute_id` = 8 OR `product_attribute`.`attribute_id` IS NULL) 
AND `product`.`provider_id` = '7' ORDER BY RAND() LIMIT 8

I was trying with left, inner and right join and nothing works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:46:06+00:00Added an answer on June 13, 2026 at 3:46 am

    You should put the condition for the left-joined table in the join, not the where clause

    ...
    from product
         left join provider ON product.provider_id = provider.id  
         left join product_attribute on product.id = product_attribute.product_id
                   and product_attribute.attribute_id = 8
    where `product`.`active` = '1' 
    and `product`.`url` IS NOT NULL  
    and `product`.`provider_id` = '7' 
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three mysql table from same database Db1. Three tables have following columns.
I have three database tables defined below, column names mentioned in parenthesis: Product (ProductID,
I have a database table with three fields: Product(ProdId, Name, Price) Where ProdId is
I have three database tables: users emails invitations Emails are linked to users by
I have three tables in a Mysql database - countries, cities and hotels. Their
In my database I have three tables: Users: UserID (Auto Numbering), UserName, UserPassword and
I have a database with three tables in my database namely catering, room, room_booking
I have three or four tables in a MySQL database associated with an upcoming
I have a database with three tables: user_table country_table city_table I want to write
I have an issue in entity framework code first, three table in database as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.