I need to count the number of times product_rec_id matches, is assigned, two or more vendor's names.
For example, when a user enters sarsa and bobbie25 I should get a value count of 1 from the example vendors' table below.
This is what I have and the strange thing, to me, is that these two methods work on the keywords' tables but not with the vendors' tables, the Count always returns a value of 0. The tables are practically similar, what could be the problem?
SELECT
Count(distinct product_rec_id)
FROM
vendors
INNER JOIN vendors_products ON vendors_products.vendor_rec_id = vendors.vendor_rec_id
WHERE
(vendors.name LIKE '%sarsa%') AND
(vendors.name LIKE '%bobbie25%')
For Firebird 3 I change Then 1
to Then True
.
SELECT
Count((CASE
WHEN vendors.name LIKE '%sarsa%'
THEN 1
END) AND (CASE
WHEN vendors.name LIKE '%bobbie25%'
THEN 1
END)) AS TRows
FROM
vendors
INNER JOIN vendors_products ON vendors_products.vendor_rec_id = vendors.vendor_rec_id
WHERE
(vendors.name LIKE '%sarsa%' OR
vendors.name LIKE '%bobbie25%')
-
CREATE TABLE vendors (
vendor_rec_id INTEGER PRIMARY KEY NOT NULL,
company_broker_id INTEGER NOT NULL,
name VARCHAR (50) NOT NULL,
vendor_id VARCHAR (50),
store_url VARCHAR (255),
website_url VARCHAR (255)
);
CREATE TABLE vendors_products (
product_rec_id INTEGER NOT NULL,
vendor_rec_id INTEGER
);
-
vendors' table data:
60 2 bobbie25 73658 http://www.somewebsite.com/ http://www.somewebsite.com/
43 2 sarsa 61688 http://www.somewebsite.com/ http://www.somewebsite.com/
-
CREATE TABLE keywords (
keyword_rec_id INTEGER PRIMARY KEYNOT NULL,
keyword VARCHAR (50) NOT NULL UNIQUE,
chart INTEGER NOT NULL
DEFAULT 0
);
CREATE TABLE keywords_products (
keyword_rec_id INTEGER NOT NULL,
product_rec_id INTEGER
);
The vendor_rec_id and name fields in vendors tables look like this, here's an example of the vendors table (there are never any null values):
vendor_rec_id name
-------------- -----------
60 bobbie25
43 sarsa
87 johnf
The vendor_rec_id and product_rec_id in vendors_products tables looks like this (one vendor name can be assigned to many products) (there are never any null values):
vendor_rec_id product_rec_id
-------------- --------------
43 1
60 1
43 2
87 3