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

  • SEARCH
  • Home
  • 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 8682187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:42:13+00:00 2026-06-12T21:42:13+00:00

I have 2 tables: goods and cats_goods (relations table). Goods: id , name .

  • 0

I have 2 tables: goods and cats_goods (relations table).

Goods: id, name.

Cats_goods: good_id, cat_id.

How to select ONLY goods which have BOTH cat_id=4 AND cat_id=24?

Have tried:

SELECT DISTINCT *
FROM `goods` AS `g`
JOIN `cats_goods` AS `cg` ON (`g`.`id` = `cg`.`good_id`)
WHERE (`cg`.`cat_id` = 24 AND `cg`.`cat_id` = 4)

Query gives 0 results!

UPD:
For each good there is 1 row in cats_goods with cat_id = 4 and one row in cats_goods with cat_id = 24. I only need to select those goods which have BOTH conditions match.

UPD2:

goods table structure:

CREATE TABLE IF NOT EXISTS `goods` (
  `id` int(11) NOT NULL auto_increment,
  `code` varchar(50) NOT NULL default '',
  `title` varchar(255) NOT NULL default '',
  `price` decimal(10,2) NOT NULL default '0.00',
  `file` varchar(50) NOT NULL default '',
  `preview` varchar(50) NOT NULL default '',
  `order` int(11) NOT NULL default '0',
  `selltype_id` int(11) NOT NULL default '0',
  `xml_date` varchar(50) NOT NULL default '',
  `invalid` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `order` (`order`),
  KEY `selltype_id` (`selltype_id`),
  KEY `code` (`code`),
  KEY `invalid` (`invalid`),
  KEY `xml_date` (`xml_date`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5014 ;

goods table data:

(4964, '00000001731', 'gold 585', 10000.00, '', '', 0, 2, '', 0),
(4965, '00000001733', 'gold 585', 10000.00, '', '', 0, 2, '', 0),
(4966, '00000001769', 'gold 585', 8000.00, '', '', 0, 2, '', 0),
(4967, '00000001767', 'gold 585', 8000.00, '', '', 0, 2, '', 0),

cats_goods table structure:

CREATE TABLE IF NOT EXISTS `cats_goods` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `good_id` int(10) unsigned NOT NULL default '0',
  `cat_id` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `good_id` (`good_id`,`cat_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=37530 ;

cats_goods table data:

(37474, 4964, 24),
(37478, 4966, 24),
(37477, 4966, 4),
(37476, 4965, 24),
(37475, 4965, 4),
(37475, 4967, 4),

ONLY goods 4965 and 4966 must be selected.

  • 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-12T21:42:14+00:00Added an answer on June 12, 2026 at 9:42 pm

    Try

    SELECT DISTINCT *
    FROM `goods` AS `g`
    JOIN `cats_goods` AS `cg1` ON (`g`.`id` = `cg1`.`good_id`)
    JOIN `cats_goods` AS `cg2` ON (`g`.`id` = `cg2`.`good_id`)
    WHERE (`cg1`.`cat_id` = 24 AND `cg2`.`cat_id` = 4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables Say Table-A and Table-B Table-A id | Article Name |date
I have two tables table: people id name goods_owned 1 john 1,4,3 2 Mike
I have two tables which I am left joining together. SELECT Col3, tab1.Col1, tab1.Col2
I have tables linked by FK, I query on the first table using entity
I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..
I have the following three tables which I am trying to perform a query
I have 3 table: [Order, Order_to_goods, Goods] I want to get of all participants
I have two tables which I would like to join by ID field. I
I need help with building SQL query. I have 4 tables: Sellers, Goods, Projects
I have two tables users registered_members I want to confirm values from user table

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.