Select value and count from different tables - mysql

I have 2 tables, one stores the user's information with a referral ID.
The other table collects records and applies the referral ID from which I arrived at that form.
I have made 2 queries that I want to join in a single query.
Example query for ID = 205391
---- Select email from refer 205391
SELECT email FROM mautic_leads WHERE refer_id IN
(SELECT refer_id FROM mautic_form_results_64_form_db_te WHERE refer_id = "205391")
---- Count results of refer 205391
(SELECT COUNT(*) AS `count` FROM `mautic_form_results_64_form_db_te` where ref = 205391)
I'm looking for the result to be:
Email | Count Ref
---------------
test#test.com 5
Table Structure:
CREATE TABLE `mautic_form_results_64_form_db_te` (
`submission_id` int(11) NOT NULL,
`form_id` int(11) NOT NULL,
`nombre` longtext COLLATE utf8_unicode_ci,
`apellido` longtext COLLATE utf8_unicode_ci,
`correo_electronico` longtext COLLATE utf8_unicode_ci,
`ref` longtext COLLATE utf8_unicode_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `mautic_form_results_64_form_db_te` (`submission_id`, `form_id`, `nombre`, `apellido`, `correo_electronico`, `ref`) VALUES
(7699, 64, 'test', 'test', 'test#test.net', '201374'),
(7700, 64, 'test2', 'test2', 'test#test.net', '205391'),
(7701, 64, 'test3', 'test3', 'test#test.net', '205391'),
(7704, 64, 'test4', 'test4', 'test#test.net', '205391'),
(7705, 64, 'test5', 'test5', 'test#test.net', '205391'),
(7706, 64, 'test6', 'test6', 'test#test.net', '201374'),
(7707, 64, 'test7', 'test7', 'test#test.net', '201374'),
(7708, 64, 'test8', ' test8 ', 'test#test.net', '205391');
CREATE TABLE `mautic_leads` (
`email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`refer_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`referido_por_usuario` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `mautic_leads` (`email`, `refer_id`, `referido_por_usuario`) VALUES
('test1#test.com', '201374', NULL),
('test2#test.com', '205388', NULL),
('test3#test.com', '205389', NULL),
('test3#test.com', '205390', NULL),
('test2#test.com', '205391', NULL);
Some help?
Thanks

You can try below query:
SELECT mautic_leads.email as email, (SELECT count(*) as count from \
mautic_form_results_64_form_db_te where ref = "205391") as count \
from mautic_leads where refer_id = "205391";
You will get the result like:
+----------------+-------+
| email | count |
+----------------+-------+
| test2#test.com | 5 |
+----------------+-------+

Now that I see your table structure, use this:
SELECT b.email,COUNT(a.submission_id) as `count`
FROM mautic_form_results_64_form_db_te a
JOIN mautic_leads b on b.refer_id = a.ref
WHERE ref = 205391

How about this?
SELECT (SELECT email
FROM mautic_leads
WHERE refer_id IN (SELECT refer_id
FROM mautic_form_results_64_form_db_te
WHERE refer_id = "205391")) as email,
Count(*) AS `count`
FROM `mautic_form_results_64_form_db_te`
WHERE ref = 205391

Related

Distinct unique value and sum others in mysql

I am trying to get the order_payment_total of the unique od_grp_id once but while using sum it get added.
CREATE TABLE IF NOT EXISTS `subscription` (
`id` int(11) unsigned NOT NULL,
`od_grp_id` int(11) unsigned NULL,
`user_id` int(11) NOT NULL,
`order_discount` decimal(10, 2) null,
PRIMARY KEY (`id`)
) DEFAULT CHARSET = utf8;
INSERT INTO `subscription` (
`id`, `od_grp_id`, `user_id`, `order_discount`
)
VALUES
(123994, NULL, 115, null),
(124255, NULL, 115, null),
(124703, 1647692222, 115, null),
(125788, 1647692312, 115, '25.00'),
(125789, 1647692312, 115, '5.00');
CREATE TABLE IF NOT EXISTS `online_payment_against_subscription` (
`subscription_od_grp_id` int(11) unsigned NOT NULL,
`order_payment_total` decimal(10, 2) unsigned NOT NULL,
`user_id` int(11) NOT NULL
) DEFAULT CHARSET = utf8;
INSERT INTO `online_payment_against_subscription` (
`subscription_od_grp_id`, `order_payment_total`, `user_id`
)
VALUES
(1643695200, '45.00', 115),
(1647692312, '250.00', 115),
(1647692222, '30.00', 115);
SELECT
sum(y.order_payment_total),
sum(s.order_discount)
FROM
subscription s
LEFT JOIN(
SELECT
SUM(order_payment_total) as order_payment_total,
user_id,
subscription_od_grp_id
FROM
online_payment_against_subscription
GROUP BY
subscription_od_grp_id
) y ON y.subscription_od_grp_id = s.od_grp_id
WHERE
find_in_set(
s.id, '123994,124255,124703,125788,125789'
)
group by
s.user_id
Current Output:
| sum(y.order_payment_total) |sum(s.order_discount) |
|----------------------------|-----------------------|
| 530 | 30 |
Expected Ouput:
| sum(y.order_payment_total) |sum(s.order_discount) |
|----------------------------|-----------------------|
| 280 | 30 |
Sql Fiddle: http://sqlfiddle.com/#!9/5628f5/1
If I understand correctly, The problem is caused by some duplicate od_grp_id from subscription table, so you might remove the duplicate od_grp_id before JOIN, so we might do that in a subquery.
Query 1:
SELECT
SUM(order_payment_total),
SUM(order_discount)
FROM (
SELECT od_grp_id,SUM(order_discount) order_discount
FROM subscription
WHERE find_in_set(id, '123994,124255,124703,125788,125789')
GROUP BY od_grp_id
) s
LEFT JOIN online_payment_against_subscription y ON y.subscription_od_grp_id=s.od_grp_id
Results:
| SUM(order_payment_total) | SUM(order_discount) |
|--------------------------|---------------------|
| 280 | 30 |
I think you are getting this error because every subscription doesn't have an order payment that is you are getting NULL values.
You can try to remove them by using this -
SELECT y.order_payment_total
FROM subscription s
LEFT JOIN(SELECT SUM(order_payment_total) AS order_payment_total, user_id, subscription_od_grp_id
FROM online_payment_against_subscription
GROUP BY subscription_od_grp_id) y ON y.subscription_od_grp_id = s.od_grp_id
WHERE FIND_IN_SET(s.id, '11258,22547,18586')
AND y.order_payment_total IS NOT NULL;
Or you can make NULL values 0 if you required -
SELECT COALESCE(y.order_payment_total, 0) AS order_payment_total
FROM subscription s
LEFT JOIN(SELECT SUM(order_payment_total) AS order_payment_total, user_id, subscription_od_grp_id
FROM online_payment_against_subscription
GROUP BY subscription_od_grp_id) y ON y.subscription_od_grp_id = s.od_grp_id
WHERE FIND_IN_SET(s.id, '11258,22547,18586');

MySQL query not acting as expected

I am trying to get all bills that have NOT been FULLY paid.
I have three tables that are needed for this.
Table 1 - billInvoiceMain
biId - unique ID
userId - users ID
type - bill or invoice
userItemId - unique ID that user chooses for their records
Table 2 - billInvoiceDetail
biId - references unique ID in billInvoiceMain
quantity
price
Table 3 - transaction
transactionId - unique ID
userId - users ID
biId - references id in billInvoiceMain
paymentAmount
So a user enters bills, and then once they make a payment (multiple smaller payments could be made on a bill until it reaches the full amount or they could make a single payment for the whole amount) they enter it and it gets saved in the transaction table.
Here is a SQL Fiddle that has abbreviated versions of test data.
CREATE TABLE IF NOT EXISTS `billInvoiceDetail` (
`biId` int(15) NOT NULL,
`productId` int(15) DEFAULT NULL,
`accountId` int(15) DEFAULT NULL,
`description` varchar(2000) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`quantity` decimal(20,3) NOT NULL,
`price` decimal(20,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `billInvoiceDetail` (`biId`, `productId`, `accountId`, `description`, `quantity`, `price`) VALUES
(51, NULL, 7, 'Pylaisiella steerei Ando & Higuchi', 4.000, 19.65),
(51, NULL, 11, 'Rubus insons L.H. Bailey', 1.000, 10.17),
(99, NULL, 11, 'Leontodon hispidus L.', 3.000, 11.99),
(99, NULL, 7, 'Peltophorum (T. Vogel) Benth.', 5.000, 33.76),
(100, NULL, 8, 'Scleria P.J. Bergius', 1.000, 10.55),
(100, NULL, 12, 'Gilia ochroleuca M.E. Jones ssp. exilis (A. Gray) A.D. Grant & V.E. Grant', 2.000, 42.54);
CREATE TABLE IF NOT EXISTS `billInvoiceMain` (
`biId` int(15) NOT NULL,
`userId` int(15) NOT NULL,
`type` varchar(7) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`cvId` int(15) NOT NULL,
`startDate` date DEFAULT NULL,
`dueDate` date DEFAULT NULL,
`userItemId` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `billInvoiceMain` (`biId`, `userId`, `type`, `cvId`, `startDate`, `dueDate`, `userItemId`) VALUES
(51, 1, 'bill', 17, '2021-01-01', '2021-01-31', '53396841'),
(99, 1, 'bill', 28, '2021-01-01', '2021-01-31', '16269083'),
(100, 1, 'bill', 28, '2021-01-07', '2021-01-17', '03200283');
CREATE TABLE IF NOT EXISTS `transaction` (
`transactionId` int(15) NOT NULL,
`userId` int(15) NOT NULL,
`biId` int(15) NOT NULL,
`paymentDate` date NOT NULL,
`paymentMethod` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`accountId` int(15) NOT NULL,
`paymentAmount` decimal(20,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `transaction` (`transactionId`, `userId`, `biId`, `paymentDate`, `paymentMethod`, `accountId`, `paymentAmount`) VALUES
(51, 1, 51, '2021-01-04', 'Check', 1, 78.60),
(52, 1, 51, '2021-01-19', 'Credit Card', 3, 10.17),
(53, 1, 99, '2021-01-14', 'Check', 1, 10.00);
SELECT billInvoiceMain.biId, SUM(transaction.paymentAmount), billInvoiceMain.useritemid
FROM billInvoiceMain
INNER JOIN transaction ON billInvoiceMain.biId = transaction.biId
WHERE billInvoiceMain.userId = 1 AND billInvoiceMain.type = 'bill'
GROUP BY billInvoiceMain.biId;
SELECT ROUND(ABS(SUM(billInvoiceDetail.price *billInvoiceDetail.quantity)),2)
FROM billInvoiceDetail
INNER JOIN billInvoiceMain ON billInvoiceDetail.biId = billInvoiceMain.biId
WHERE billInvoiceMain.userId=1 AND billInvoiceMain.type = 'bill'
GROUP BY billInvoiceMain.biId;
SELECT billInvoiceMain.biId, billInvoiceMain.useritemid
FROM billInvoiceMain
INNER JOIN transaction ON billInvoiceMain.biId = transaction.biId
INNER JOIN billInvoiceDetail ON billInvoiceDetail.biId = transaction.biId
WHERE billInvoiceMain.userId = 1 AND billInvoiceMain.type = 'bill'
HAVING SUM(transaction.paymentAmount) != ROUND(ABS(SUM(billInvoiceDetail.price *billInvoiceDetail.quantity)),2);
The first query allows me to sum of all the payments from transaction grouped by bill id.
The second query sums all the bills.
The third query I tried combing the two. However, when I try to use a GROUP BY, it gives an error. So, I got rid of that and now it just returns the first bill even if it has been paid.
Desired Results (retrieves the biId and userItemId of all bills that have not been fully paid based on the transaction table):
biId
userItemId
99
16269083
100
03200283
I have spent a lot of time trying to figure this out but am lost.
The following query retrieves rows that don't match the biId obtained by joining the result of sum of all the payments from transaction grouped by bill id and the result of sum of all the bills from billInvoiceDetail grouped by bill id.
SELECT biId, useritemid FROM billInvoiceMain
WHERE userId = 1 AND type = 'bill'
AND biId NOT IN(
SELECT t.biId FROM
(SELECT biId,SUM(paymentAmount) pay FROM transaction GROUP BY biId) t
INNER JOIN
(SELECT biId,ROUND(ABS(SUM(price*quantity)),2) bill FROM billInvoiceDetail GROUP BY biId) d
ON t.biId=d.biId AND t.pay=d.bill
)
SQL Fiddle

mysql GROUB BY idea

I have the following scenario: there are 1 table with books and two couples of tables (HD/IT) with Sales Order and Purchase Order transactions connecting through Sales Order id.
The table structure follows:
CREATE TABLE `books` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`isbn` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`it_id` int(11) NOT NULL,
`kind` tinyint(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `books` (`id`, `isbn`, `it_id`, `kind`) VALUES
(1, '12345', 1, 1),
(2, '12345', 1, 2),
(3, '67890', 2, 1),
(4, '1111111', 2, 2);
CREATE TABLE `porders_hd` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date NOT NULL,
`so_id` int(11) DEFAULT NULL,
`customer` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `porders_hd` (`id`, `dt`, `so_id`, `customer`) VALUES
(1, '2017-07-02', 1, 1),
(2, '2017-08-03', NULL, 3);
CREATE TABLE `porders_it` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hd_id` int(11) NOT NULL,
`isbn` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`dscr` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`qty` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `porders_it` (`id`, `hd_id`, `isbn`, `dscr`, `qty`) VALUES
(1, 1, '12345', 'Book 1', 1),
(2, 2, '1111111', 'Book 2', 1);
CREATE TABLE `sorders_hd` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dt` date NOT NULL,
`customer` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `sorders_hd` (`id`, `dt`, `customer`) VALUES
(1, '2017-07-01', 1),
(2, '2017-08-01', 2);
CREATE TABLE `sorders_it` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hd_id` int(11) NOT NULL,
`isbn` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`dscr` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`qty` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `sorders_it` (`id`, `hd_id`, `isbn`, `dscr`, `qty`) VALUES
(1, 1, '12345', 'Book 1', 1),
(2, 2, '67890', 'Book 2', 1);
In summary there are:
* 1 Sales Order (#1) also existing in the Purchase Order (#1)
* 1 Sales Order (#2) still pending
* 1 Purchase Order (#2) created without a Sales Order
I want to be able to grab all Sales and Purchases Order per book's isbn and the connected SO and PO must be in the same line. The output must be like the one below:
so_id so_date po_id po_date isbn dscr
NULL NULL 2 2017-08-03 1111111 Book 2
1 2017-07-01 1 2017-07-02 12345 Book 1
2 2017-08-01 NULL NULL 67890 Book 3
I tried to grab the rows using a query like the one below:
SELECT
GROUP_CONCAT(so_id) so_id,
GROUP_CONCAT(so_date) so_date,
GROUP_CONCAT(po_id) po_id,
GROUP_CONCAT(po_date) po_date,
isbn,
dscr
FROM (
SELECT
hd.so_id so_id,
NULL so_date,
hd.id po_id,
hd.dt po_date,
bk.isbn,
it.dscr
FROM porders_hd hd,
porders_it it,
books bk
WHERE it.hd_id = hd.id
AND bk.isbn = it.isbn
AND kind = 2
UNION
SELECT
hd.id so_id,
hd.dt so_date,
NULL po_id,
NULL po_date,
bk.isbn,
it.dscr
FROM sorders_hd hd,
sorders_it it,
books bk
WHERE it.hd_id = hd.id
AND bk.isbn = it.isbn
AND kind = 1
) as table1
GROUP BY isbn, so_id, po_id
but since there is info missing I get the following result:
so_id so_date po_id po_date isbn dscr
NULL NULL 2 2017-08-03 1111111 Book 2
1 2017-07-01 NULL NULL 12345 Book 1
1 NULL 1 2017-07-02 12345 Book 1
2 2017-08-01 NULL NULL 67890 Book 3
Any ideas how can I achieve this ?
I think this is what you're after, but I can;t figure out the role of kind from your code. But here is a query that for each books, gets the associated po line item, finds the corresponding so line item and joins the header rows so the dates are available. Note my assumption that a sales order can't exist with a corresponding PO.
SELECT books.isbn, books.descr, sorders_hd.id, sorders_hd.dt, porders_hd.id, porders_hd.dt
FROM book
join porders_it on porders_it.isbn = books.isbn
join porders_hd on porders_hd.id = porders_it.hd_id
left outer join sorders_it on sorders_it.hd_id=porders_hd.so_id and sorders_it.isbn = porders_it.isbn
left outer join sorders_hd on sorders_hd.id = sorders_it.hd_it
You could normalize your tables so that descr need not be repeated, and also use the book.id in the other tables rather than isbn.
I'm adding a new answer because the previous one and the comments are illustrative. Based on that discussion, this requires a FULL OUTER JOIN which must be emulated by UNION ALL in mysql (which may be what OP was attempting originally).
Here is my new code, taking that into account:
SELECT sorders_hd.id as so_id, sorders_hd.dt as so_dt,
porders_hd.id as po_id, porders_hd.dt as po_dt,
books.isbn, porders_it.dscr
from books
left outer join porders_it on porders_it.isbn=books.isbn
join porders_hd on porders_hd.id=porders_it.hd_id
left outer join sorders_it on sorders_it.isbn=books.isbn and sorders_it.hd_id=porders_hd.so_id
left outer join sorders_hd on sorders_hd.id=sorders_it.hd_id
where books.kind=2
UNION ALL
SELECT sorders_hd.id as so_id, sorders_hd.dt as so_dt,
porders_hd.id as po_id, porders_hd.dt as po_dt,
books.isbn, sorders_it.dscr
from books
left outer join sorders_it on sorders_it.isbn=books.isbn
join sorders_hd on sorders_hd.id=sorders_it.hd_id
left outer join porders_it on porders_it.isbn=books.isbn
left outer join porders_hd on porders_hd.id=porders_it.hd_id and porders_hd.so_id=sorders_hd.id
where porders_hd.id is null and books.kind=1;
The output result is:
so_id so_dt po_id po_dt isbn dscr
1 2017-07-01 1 2017-07-02 12345 Book 1
(null) (null) 2 2017-08-03 1111111 Book 2
2 2017-08-01 (null) (null) 67890 Book 2
See SqlFiddle
The "trick" is to use union all with one of the two queries excluding records that linked both sides (to get the 'right' side of the FULL OUTER JOIN)
+1 to OP for providing the DDL and sample data!
I agree that the data model could be reworked, and could be normalized. The existing model still has at least the problem of a duplicate book record when a sales order and purchase order match (one of them is ignored). It seems to me that one improvement would be to have a master book list and include the id (or isbn if that is the primary key) from that table in porders_it and sorders_it, and eliminate the current books table.

how to match in mysql

I have two tables, one for candidates and their skills, and the other for jobs and the skills required for the job, such as the ones shown below:
CSID = candidate skill ID (PK)
CID = candidate ID (FK)
S_CODE = skill code (FK)
Here is the candidate_skill table
+------------+---------+---------+
| CSID | CID | S_CODE |
+------------+---------+---------+
| 1 | 1 | 5 |
| 2 | 1 | 9 |
| 3 | 2 | 5 |
| 4 | 2 | 10 |
+------------+---------+---------+
SJID = skill job ID (PK)
JID = job ID (FK)
S_CODE = skill code (FK)
Here is the skill_job table:
+------------+---------+---------+
| SJID | JID | S_CODE |
+------------+---------+---------+
| 12 | 50 | 5 |
| 13 | 50 | 9 |
| 14 | 51 | 1 |
| 15 | 52 | 10 |
+------------+---------+---------+
So in this example, the only candidate would be 1 for the job 50, since the skill codes (S_CODE) are 5 and 9 for both, but i also would like candidate 2 to be a match to 52, since it has the required job skill and an extra one.
I have tried several ways to match the job and candidate but I'm failing, an example of such a way is the code below:
SELECT * , COUNT(skill_job.S_CODE) AS cnt FROM candidate_skill,
skill_job WHERE candidate_skill.S_CODE = skill_job.S_CODE HAVING (cnt >=2)
the problem is, that only limits the candidate found to one and if i remove the COUNT clause, it lists all candidates that match with only one skill so candidate 2 would also be matched to the job 50.
Here is the mysql code for the tables from phpmyadmin:
-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 16, 2012 at 09:27 PM
-- Server version: 5.5.16
-- PHP Version: 5.3.8
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `employment`
--
-- --------------------------------------------------------
--
-- Table structure for table `candidate`
--
CREATE TABLE IF NOT EXISTS `candidate` (
`CID` int(4) NOT NULL AUTO_INCREMENT,
`title` varchar(5) NOT NULL,
`fname` varchar(30) NOT NULL,
`lname` varchar(30) NOT NULL,
`dob` date NOT NULL,
`email` varchar(50) NOT NULL,
`address` varchar(255) NOT NULL,
`city` varchar(50) NOT NULL,
`postcode` varchar(10) NOT NULL,
`phone_num` varchar(11) NOT NULL,
`username` varchar(40) NOT NULL,
`password` varchar(40) NOT NULL,
`regdate` datetime NOT NULL,
`acc_type` enum('c','s') NOT NULL DEFAULT 'c',
`emailactivate` enum('0','1') NOT NULL DEFAULT '0',
`cv_name` varchar(60) NOT NULL,
`cv` blob NOT NULL,
PRIMARY KEY (`CID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='// this is the table for the candidates' AUTO_INCREMENT=175 ;
--
-- Dumping data for table `candidate`
--
INSERT INTO `candidate` (`CID`, `title`, `fname`, `lname`, `dob`, `email`, `address`, `city`, `postcode`, `phone_num`, `username`, `password`, `regdate`, `acc_type`, `emailactivate`, `cv_name`, `cv`) VALUES
(128, 'Mr', 'clement', 'Chilingulo', '0000-00-00', 'chlngl#yahoo.com', '28 oakfield Road', 'london', 'E6 1LW', '07771611873', 'casante', 'b59c67bf196a4758191e42f76670ceba', '0000-00-00 00:00:00', 'c', '0', '', '');
INSERT INTO `candidate` (`CID`, `title`, `fname`, `lname`, `dob`, `email`, `address`, `city`, `postcode`, `phone_num`, `username`, `password`, `regdate`, `acc_type`, `emailactivate`, `cv_name`, `cv`) VALUES
(134, 'Mr', 'rverv', 'revrb', '0000-00-00', 'tdbsdrt', 'trsbrtd', 'trbtrtrb', 'tbrfbgrts', 'trfbtrgbrfg', 'clement', 'b59c67bf196a4758191e42f76670ceba', '0000-00-00 00:00:00', 's', '0', '', '');
INSERT INTO `candidate` (`CID`, `title`, `fname`, `lname`, `dob`, `email`, `address`, `city`, `postcode`, `phone_num`, `username`, `password`, `regdate`, `acc_type`, `emailactivate`, `cv_name`, `cv`) VALUES
(165, 'Mr', 'oinInINOioni', 'ioin', '0000-00-00', 'inioimn', 'in', 'oin', 'oni', 'io', 'k', '7b8b965ad4bca0e41ab51de7b31363a1', '0000-00-00 00:00:00', 'c', '0', '', ''),
(166, 'Mr', 'pjINoNlkinoinoi', 'ino', '0000-00-00', 'oimpnponi', 'inoi', 'no', 'nj', 'nio', 'nio', 'eb5bc837d01b911029ae378e8a1c9f5d', '0000-00-00 00:00:00', 'c', '0', '', ''),
(167, 'Mr', 'vrae', 'ergvaer', '0000-00-00', 'aerbg', 'aergvera', 'aergvrea', 'aergvear', 'aergarev', 'grebvarvf', '609c1b136ec8d0d2dfdf9a2105fb605f', '0000-00-00 00:00:00', 'c', '0', '', ''),
(168, 'Mr', 'vrae', 'ergvaer', '0000-00-00', 'aerbg', 'aergvera', 'aergvrea', 'aergvear', 'aergarev', 'grebvarvf', '609c1b136ec8d0d2dfdf9a2105fb605f', '0000-00-00 00:00:00', 'c', '0', '', ''),
(169, 'Mr', 'ubp', 'bu', '0000-00-00', 'ubip', ';ub', 'ubi', 'ubo', 'buo', 'ubiipbu', '9f44ce1389a3e7372834ed730b559a5e', '0000-00-00 00:00:00', 'c', '0', '', ''),
(170, 'Mr', 'rvev', 'ferhbgetb', '0000-00-00', 'tsbtrb', 'trbstrb', 'trbsfb ', 'stb stb', 'vvfs', 'csdcdsvarewdcv', 'd41d8cd98f00b204e9800998ecf8427e', '0000-00-00 00:00:00', 'c', '0', '', ''),
(171, 'Mr', 'rvev', 'ferhbgetb', '0000-00-00', 'tsbtrb', 'trbstrb', 'trbsfb ', 'stb stb', 'vvfs', 'csdcdsvarewdcv', 'd41d8cd98f00b204e9800998ecf8427e', '0000-00-00 00:00:00', 'c', '0', '', ''),
(172, '', '', '', '0000-00-00', '', '', '', '', '', '', 'd41d8cd98f00b204e9800998ecf8427e', '0000-00-00 00:00:00', 'c', '0', '', ''),
(173, '', 'dds', 'vrv av', '0000-00-00', 'rvear', 'vreverav', 'rvedfsdv', 'frvdvf', 'fveavd', 'vdrareavdv', '84c71ac76340092398a1ed4bc7d6fe19', '0000-00-00 00:00:00', 'c', '0', '', ''),
(174, '', 'dds', 'vrv av', '0000-00-00', 'rvedwear', 'vreverav', 'rvedfsdv', 'frvdvf', 'fveavd', 'vdraredfcavdv', 'd41d8cd98f00b204e9800998ecf8427e', '0000-00-00 00:00:00', 'c', '0', '', '');
-- --------------------------------------------------------
--
-- Table structure for table `candidate_skill`
--
CREATE TABLE IF NOT EXISTS `candidate_skill` (
`CSID` int(4) NOT NULL AUTO_INCREMENT,
`CID` int(4) NOT NULL,
`S_CODE` int(4) NOT NULL,
PRIMARY KEY (`CSID`),
KEY `CID` (`CID`),
KEY `S_CODE` (`S_CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='//match candidate and skill' AUTO_INCREMENT=131 ;
--
-- Dumping data for table `candidate_skill`
--
INSERT INTO `candidate_skill` (`CSID`, `CID`, `S_CODE`) VALUES
(114, 134, 2),
(116, 134, 9),
(121, 134, 5),
(126, 128, 1);
-- --------------------------------------------------------
--
-- Table structure for table `job`
--
CREATE TABLE IF NOT EXISTS `job` (
`JID` int(4) NOT NULL AUTO_INCREMENT,
`job_title` varchar(40) NOT NULL,
`job_desc` varchar(255) NOT NULL,
`start_date` date NOT NULL,
`end_date` date NOT NULL,
PRIMARY KEY (`JID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='// this is the table for the job vacancies' AUTO_INCREMENT=10 ;
--
-- Dumping data for table `job`
--
INSERT INTO `job` (`JID`, `job_title`, `job_desc`, `start_date`, `end_date`) VALUES
(1, 'engineer', ' fix engineering ish that is messed ', '0000-00-00', '0000-00-00'),
(5, 'pilot', '', '0000-00-00', '0000-00-00'),
(8, 'Charity Helper', ' ', '0000-00-00', '0000-00-00'),
(9, 'accountant', ' ', '0000-00-00', '0000-00-00');
-- --------------------------------------------------------
--
-- Table structure for table `skill`
--
CREATE TABLE IF NOT EXISTS `skill` (
`S_CODE` int(4) NOT NULL AUTO_INCREMENT COMMENT '// this is the skill primary key',
`skill_name` varchar(40) NOT NULL,
`skill_desc` varchar(255) NOT NULL,
PRIMARY KEY (`S_CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `skill`
--
INSERT INTO `skill` (`S_CODE`, `skill_name`, `skill_desc`) VALUES
(1, 'speaking', 'English is your first language'),
(2, ' writing', 'You can write English very well'),
(3, ' public', ''),
(5, 'initiative', ''),
(6, 'interviewing', ''),
(7, 'negotiating', ''),
(8, 'leading', ''),
(9, ' energy', ''),
(10, ' organisation', '');
-- --------------------------------------------------------
--
-- Table structure for table `skill_job`
--
CREATE TABLE IF NOT EXISTS `skill_job` (
`SJID` int(4) NOT NULL AUTO_INCREMENT,
`JID` int(4) NOT NULL,
`S_CODE` int(4) NOT NULL,
PRIMARY KEY (`SJID`),
KEY `S_CODE` (`S_CODE`),
KEY `JID` (`JID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=106 ;
--
-- Dumping data for table `skill_job`
--
INSERT INTO `skill_job` (`SJID`, `JID`, `S_CODE`) VALUES
(91, 5, 2),
(94, 5, 5),
(95, 5, 9),
(98, 1, 1),
(102, 1, 8),
(105, 8, 8);
--
-- Constraints for dumped tables
--
--
-- Constraints for table `candidate_skill`
--
ALTER TABLE `candidate_skill`
ADD CONSTRAINT `candidate_skill_ibfk_3` FOREIGN KEY (`CID`) REFERENCES `candidate` (`CID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `candidate_skill_ibfk_4` FOREIGN KEY (`S_CODE`) REFERENCES `skill` (`S_CODE`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `skill_job`
--
ALTER TABLE `skill_job`
ADD CONSTRAINT `skill_job_ibfk_4` FOREIGN KEY (`JID`) REFERENCES `job` (`JID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `skill_job_ibfk_5` FOREIGN KEY (`S_CODE`) REFERENCES `skill` (`S_CODE`) ON DELETE CASCADE ON UPDATE CASCADE;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Double negation is your friend (though I don't know MySQL well enough to tell whether it supports it):
SELECT DISTINCT CS.CID, SJ.JID
FROM CANDIDATE_SKILL CS
JOIN SKILL_JOB SJ ON CS.S_CODE = SJ.S_CODE
WHERE NOT EXISTS(SELECT 1
FROM SKILL_JOB SJ2
WHERE SJ.JID = SJ2.JID
AND NOT EXISTS(SELECT 1
FROM CANDIDATE_SKILL CS2
WHERE CS.CID = CS2.CID
AND SJ2.S_CODE = CS2.S_CODE))
A 'human translation' of this is to say that there should not exists any skill required for the job that the candidate doesn't have.
It seems like you'll need to use an INNER JOIN to combine your two tables and then compare their row values.
Should be something like this..
SELECT
CID
FROM
candidate_skill
WHERE
S_CODE IN (
SELECT S_CODE FROM skill_jobs where JID = 52
)
GROUP BY CID
CORRECTION: Previous query will fail to match all required S_CODE
SELECT
CID, COUNT(CSID) as cnt
FROM
candidate_skill
INNER JOIN skill_jobs
WHERE
S_CODE IN (
SELECT S_CODE FROM skill_jobs where JID = 52
)
GROUP BY CID
HAVING cnt = (
SELECT count(*) from skill_jobs where JID = 52
)
You will need a bit more complicated JOIN, and I see no easy way of simultaneously answering your request and the obvious ones "How is candidate X matched for job Y?" and "HOW MUCH does candidate X fit job Y?".
Anyway, here goes.
SELECT candidate_skill.CID, skill_job.JID, COUNT(*) AS has, sjtot.needed
FROM skill_job JOIN candidate_skill ON (candidate_skill.S_CODE = skill_job.S_CODE)
LEFT JOIN ( SELECT JID, COUNT(*) AS needed FROM skill_job GROUP BY JID ) AS sjtot
ON ( skill_job.JID = sjtot.JID )
GROUP BY CID, JID
HAVING has >= needed;
In practice I first group the required skills on skill_job, and this tells me that job 50 requires two skills.
Armed with this, I LEFT JOIN required and candidate skills; some candidates will join on all skills, some won't. It is then just a matter of counting how many do match.
+------+------+-----+--------+
| CID | JID | has | needed |
+------+------+-----+--------+
| 1 | 50 | 2 | 2 |
| 2 | 52 | 1 | 1 |
+------+------+-----+--------+
On second thought, required skill level and offered skill levels might be selected, and CASE WHEN used to extract a match (1 if offered >= required, offered/required otherwise). The average of the resulting values might then be used as a "skill match" index.
As for the "How is X matched for Y?" question, once you know that candidate CID 1 is good for job JID 50, you can run a LEFT JOIN between the candidates and the jobs. You already know that whatever the jobs asks, the candidate has, but this way you retrieve what unasked-for skills the candidate has, and the other relevant values as well.
A nested query joining the query above and this last one would perhaps tell you everything in one fell swoop, but it would be a costly swoop, I think :-)
If you don't care about cross-dbms support, you can use mysql specific group_concat to reduce the two s_code sets to strings, one per candidate/job and just compare them like this:
select * from
(select cs.cid, group_concat(cs.s_code order by cs.s_code) skills
from candidate_skill cs group by cs.cid) cs,
(select sj.jid, group_concat(sj.s_code order by sj.s_code) skills
from skill_job sj group by sj.jid) sj
where sj.skills = cs.skills
Be aware of that when your tables grown the the final where will be slower and slower and you can't create indexes on the group_concat() created fields.

MySQL filter query with relation

I'm having the following problem with 2 MySQL tables that have a relation:
I can easily query table 1 (address) when I want a full list or filter the result by name or email or such. But now I need to query table 1 and filter it based on the relational content of table 2 (interests). So, I need to find a row (usually many rows) in table 1 only if a (or more) conditions are met in table 2.
Here are the tables:
CREATE TABLE IF NOT EXISTS `address` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`countryCode` char(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`languageCode` char(2) COLLATE utf8_unicode_ci DEFAULT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `emailUnique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
INSERT INTO `address` (`id`, `name`, `email`, `countryCode`, `languageCode`, `timestamp`) VALUES
(1, '', 'dummy#test.com', 'BE', 'nl', '2010-07-16 14:07:00'),
(2, '', 'test#somewhere.com', 'BE', 'fr', '2010-07-16 14:10:25');
CREATE TABLE IF NOT EXISTS `interests` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`address_id` int(11) unsigned NOT NULL,
`cat` char(2) COLLATE utf8_unicode_ci NOT NULL,
`subcat` char(2) COLLATE utf8_unicode_ci NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `address_id` (`address_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
INSERT INTO `interests` (`id`, `address_id`, `cat`, `subcat`, `timestamp`) VALUES
(1, 1, 'aa', 'xx', '2010-07-16 14:07:00'),
(2, 1, 'aa', 'yy', '2010-07-16 14:07:00'),
(3, 2, 'aa', 'xx', '2010-07-16 14:07:00'),
(4, 2, 'bb', 'zz', '2010-07-16 14:07:00')
(5, 2, 'aa', 'yy', '2010-07-16 14:07:00');
ALTER TABLE `interests`
ADD CONSTRAINT `interests_ibfk_1` FOREIGN KEY (`address_id`) REFERENCES `address` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
For example, I need to find the address(es) that has (have) as interest cat=aa and subcat=xx. Or, another example, I need the address(es) with as interest both cat=aa and subcat=xx AND cat=aa and subcat=yy. Specially the latter is important and one has to keep in mind that both the address and the interest tables will be long lists and that the amount of cat/subcat combinations will vary. I'm working with reference queries through Zend_Db_Table (findDependentRowset) at the moment but that solution is way to slow for address lists numbering 100s and even 1000s of hits.
Thank you for your help.
SELECT a.name FROM address a
INNER JOIN interests i ON (a.id = i.address_id)
WHERE i.cat = "aa" AND i.subcat IN ('xx', 'yy')
I added another row in your interests table, to demonstrate a different result set between the two examples:
INSERT INTO interests VALUES (6, 2, 'aa', 'vv', '2010-07-16 14:07:00');
Then you may want to try using correlated subqueries as follows:
SELECT *
FROM address a
WHERE EXISTS (SELECT id
FROM interests
WHERE address_id = a.id AND
(cat = 'aa' and subcat = 'xx'));
Result:
+----+------+--------------------+-------------+--------------+---------------------+
| id | name | email | countryCode | languageCode | timestamp |
+----+------+--------------------+-------------+--------------+---------------------+
| 1 | | dummy#test.com | BE | nl | 2010-07-16 14:07:00 |
| 2 | | test#somewhere.com | BE | fr | 2010-07-16 14:10:25 |
+----+------+--------------------+-------------+--------------+---------------------+
2 rows in set (0.00 sec)
For the second example, we're testing for the new row added previously in order not to have the same result as above:
SELECT *
FROM address a
WHERE EXISTS (SELECT id
FROM interests
WHERE address_id = a.id AND
(cat = 'aa' and subcat = 'xx')) AND
EXISTS (SELECT id
FROM interests
WHERE address_id = a.id AND
(cat = 'aa' and subcat = 'vv'));
Result:
+----+------+--------------------+-------------+--------------+---------------------+
| id | name | email | countryCode | languageCode | timestamp |
+----+------+--------------------+-------------+--------------+---------------------+
| 2 | | test#somewhere.com | BE | fr | 2010-07-16 14:10:25 |
+----+------+--------------------+-------------+--------------+---------------------+
1 row in set (0.00 sec)
Using correlated subqueries is easy and straightforward. However keep in mind that it might not be the best in terms of performance, because the correlated subqueries will be executed once for each address in the outer query.