I have two tables, they look roughly like below:
Categories:
+----+-----------+--------+----------------+
| id | entity_id | set_id | type |
+----+-----------+--------+----------------+
| 1 | 49 | 1 | signup |
| 2 | 57 | 1 | signup |
| 3 | 65 | 1 | signup |
| 4 | 69 | 1 | recommendation |
| 5 | 73 | 1 | signup |
| 6 | 77 | 1 | recommendation |
| 7 | 23 | 2 | comment |
| 8 | 45 | 2 | recommendation |
| 9 | 56 | 2 | signup |
| 10| 76 | 2 | signup |
+----+-----------+--------+----------------+
Steps:
+----+--------+----------+--------+
| id | set_id | start_id | end_id |
+----+--------+----------+--------+
| 1 | 1 | 49 | 57 |
| 2 | 1 | 57 | 65 |
| 3 | 1 | 65 | 69 |
| 4 | 1 | 69 | 73 |
| 5 | 1 | 77 | 57 |
| 6 | 2 | 23 | 45 |
| 7 | 2 | 45 | 56 |
| 8 | 2 | 56 | 76 |
+----+--------+----------+--------+
I need to select the rows of a given set_id from categories, but only if their entity_id is found in steps in the start_id column but NOT the end_id column.
So from this example, these are the two records I would expect to be returned:
+----+-----------+--------+----------------+
| id | entity_id | set_id | type |
+----+-----------+--------+----------------+
| 1 | 49 | 1 | signup |
| 6 | 77 | 1 | recommendation |
+----+--------+----------+-----------------+
I know how to perform a basic join on set_id, but I'm not sure how to search all the records of that set_id from steps and exclude those that have an entity_id that appears in end_id
Something like:
SELECT * FROM categories JOIN steps ON categories.set_id = steps.set_id WHERE ....categories.entity_id is present in steps.start_id but not in steps.end_id??
How should this logic be properly formed?
Exists logic works well here and is also straightforward:
SELECT c.*
FROM categories c
WHERE
EXISTS (SELECT 1 FROM steps s WHERE s.set_id = c.set_id AND c.entity_id = s.start_id)
AND
NOT EXISTS (SELECT 1 FROM steps s WHERE s.set_id = c.set_id AND c.entity_id = s.end_id);
The above query, read in plain English, says to find all category records such that there exists a matching record in steps where the entity_id matches the start_id, but there does not also exist a record in steps where the entity_id appears in end_id.
I have following statement that is used to select some fields from MySQL DB
select finance_budget_issue.budget_date, SUM(finance_budget_issue.amount) AS amount, finance_vote.office_id as vote_office_id, finance_office.office_head as head,
finance_office.office_name AS office_name,
finance_budget.ref_no, finance_budget_issue.view_status, tbl_signature.office_head as sign_office_head, tbl_signature.name AS name,
tbl_signature.post AS post, tbl_signature.sign_id
from finance_budget_issue
inner join finance_budget on finance_budget.budget_id=finance_budget_issue.budget_id
left join finance_vote on finance_budget_issue.vote_id=finance_vote.vote_id
left join finance_vote_description on finance_vote.description=finance_vote_description.vote_description_id
left join finance_office on finance_budget_issue.office=finance_office.office_id
left join tbl_signature on finance_office.office_id=tbl_signature.office_id
The statement is working fine, but didn't outs the following fields
tbl_signature.office_head as sign_office_head,
tbl_signature.name AS name,
tbl_signature.post AS post
What may be going wrong ? I think that I used incorrect Joins. Can anyone help ?
Tables as follows :
finance_office
+----+-----------+-------------+------+
| id | office_id | office_name | head |
+----+-----------+-------------+------+
| 1 | 48 | A | SS |
| 2 | 69 | B | VV |
+----+-----------+-------------+------+
finance_vote
+---------+-----------+----------------+
| vote_id | office_id | vote |
+---------+-----------+----------------+
| 1 | 48 | 320-1-2-1-1001 |
| 2 | 48 | 320-2-2-2-2002 |
| 3 | 69 | 319-1-2-1-1001 |
| 4 | 69 | 319-1-2-2-1102 |
| 5 | 30 | 318-1-1-2-1101 |
+---------+-----------+----------------+
tbl_signature
+---------+-----------+---------+------------+-------------+
| sign_id | office_id | name | post | office_head |
+---------+-----------+---------+------------+-------------+
| 1 | 48 | Noel | Accountant | Manager |
| 2 | 69 | Jhon | Accountant | Manager |
| 3 | 30 | Micheal | Accountant | Manager |
+---------+-----------+---------+------------+-------------+
finance_budget
+-----------+--------+-------------+
| budget_id | ref_no | budget_date |
+-----------+--------+-------------+
| 1 | Acc/01 | 2020-01-20 |
| 2 | Acc/02 | 2020-01-22 |
+-----------+--------+-------------+
finance_budget_issue
+----+-----------+--------+---------------+-----------------+
| id | budget_id | amount | budget_status | transfer_status |
+----+-----------+--------+---------------+-----------------+
| 1 | 1 | 75000 | issues | Approved |
| 2 | 1 | 22000 | issues | Approved |
| 3 | 2 | 65000 | issues | Approved |
+----+-----------+--------+---------------+-----------------+
Desired Output
+--------+----------------+------+--------+------------------+------+------------+
| amount | vote_office_id | head | ref_no | sign_office_head | name | post |
+--------+----------------+------+--------+------------------+------+------------+
| 75000 | 48 | SS | Acc/01 | Manager | Noel | Accountant |
| 22000 | 48 | SS | Acc/01 | Manager | Noel | Accountant |
| 65000 | 69 | VV | Acc/02 | Manager | Jhon | Accountant |
+--------+----------------+------+--------+------------------+------+------------+
Generated Output (Incorrect)
+--------+----------------+------+--------+------------------+------+------+
| amount | vote_office_id | head | ref_no | sign_office_head | name | post |
+--------+----------------+------+--------+------------------+------+------+
| 75000 | 48 | SS | Acc/01 | | | |
| 22000 | 48 | SS | Acc/01 | | | |
| 65000 | 69 | VV | Acc/02 | | | |
+--------+----------------+------+--------+------------------+------+------+
This is easier to read:
SELECT i.budget_date
, SUM(i.amount) amount
, v.office_id vote_office_id
, o.office_head head
, o.office_name
, b.ref_no
, i.view_status
, s.office_head sign_office_head
, s.name
, s.post
, s.sign_id
FROM finance_budget_issue i
JOIN finance_budget b
ON b.budget_id = i.budget_id
LEFT
JOIN finance_vote v
ON v.vote_id = i.vote_id
LEFT
JOIN finance_vote_description d
ON d.vote_description_id = v.description
LEFT
JOIN finance_office o
ON i.office = o.office_id
LEFT
JOIN tbl_signature s
ON s.office_id = o.office_id
You have an aggregate function (and non-aggregated columns) but no GROUP BY clause; that's not going to work. You have a LEFT JOINed table from which you select no columns; that's pointless.
For further help, see Why should I provide an MCRE for what seems to me to be a very simple SQL query
I wrote this query to find the list of shops selling what categories.
SELECT GROUP_CONCAT(distinct(sub_category_id)) AS s,
vendor_id AS v
FROM link_products_lists_vendors
GROUP BY vendor_id;
This results in,
+---------------------------------------+------------+
| category_ids | vendor_ids |
+---------------------------------------+------------+
| 24,28,25,16,26,23,27,2 | 3 |
| 2 | 67 |
| 19,28,17,16,20,2 | 68 |
| 19,28,24,26,23,21,16,27,22,17,25,2 | 122 |
| 16,2 | 123 |
| 28,17,22,21,18,16,26,27,20,23,25,2 | 124 |
| 22,19,21,20,16,24,28,25,23,26,2 | 125 |
| 23,24,26,25,28,16,20,27,19,2 | 126 |
| 19,26,28,18,20,27,22,16 | 127 |
| 22,26,28,21,23,20,24,19,16,17,27,25,2 | 128 |
| 2 | 129 |
| 2 | 133 |
| 19,20,28,16,27,25,21,23,26,24,22 | 135 |
| 23,28,17,22,26,21,16,20,27,24,25,2 | 136 |
| 19,17,16,21,23,26,22,25,27,20,28 | 137 |
| 19,20,26,22,21,24,23,17,28,16,27,25,2 | 138 |
| 19,20,23,28,26,21,24,16,27,22,25,17,2 | 139 |
| 22,27,20,21,24,17,23,28,26,19,25,2 | 142 |
| 19,28,17,20,2 | 143 |
+---------------------------------------+------------+
19 rows in set (0.01 sec)
What I want now is something like,
+-------------------------- -----------+--- ----------+
| category_names | vendor_names |
+---------------------------------------+--------------+
| mobiles,laptops,desktops | abcShop |
| mobiles | xyzShop |
| desktops,mouses,keyboards | pqrShop |
+---------------------------------------+--------------+
I have the categories table as,
+----+---------------+
| id | name |
+----+---------------+
| 17 | desktops |
| 18 | external_hdds |
| 26 | headphones |
| 27 | headsets |
| 22 | keyboards |
| 16 | laptops |
| 24 | memory_cards |
| 2 | mobile-phones |
| 21 | mouses |
| 25 | pendrives |
| 19 | printers |
| 20 | routers |
| 23 | speakers |
| 28 | tablets |
+----+---------------+
Vendors table as,
+-----+----------------------+
| id | name |
+-----+----------------------+
| 108 | abcShop |
| 109 | xyzShop |
| 45 | pqrShop |
| 89 | . |
| 63 | . |
| 64 | . |
+-----+----------------------+
How should I write a query that will not display ids but use the table that displays ids and displays names? I have no clue from where to start this. Please help!
Just join in the other tables and select those values instead
SELECT GROUP_CONCAT(distinct(c.name)) AS s,
v.name AS v
FROM link_products_lists_vendors l
JOIN categories c on l.category_id = c.id
JOIN vendors v on l.vendor_id = v.id
GROUP BY v.name;
This works as long as vendor names and description names are unique.
Maybe this will be an easy one for some of you MySQL masters who see this stuff like a level 3 children's book.
I have multiple tables that I'm joining to produce statistical data for a report and I'm getting tripped up at the moment trying to figure it out. It's obviously imperative the figures are correct because it impacts a number of decisions going forward.
Here's the lay of the land (not the full picture, but you'll get the point):
Affiliate Table
+----+-----------+------------+---------------------+
| id | firstname | lastname | created_date |
+----+-----------+------------+---------------------+
| 1 | Mike | Johnson | 2010-11-22 17:44:37 |
| 2 | Trevor | Wilson | 2010-12-23 16:24:24 |
| 3 | Bob | Parker | 2011-11-04 10:33:49 |
+----+-----------+------------+---------------------+
Now our query should only find results for Bob Parker (id 3) so I'll only show example results for Bob.
Affiliate Link Table
+-----+-----------+--------------+-----------+----------+---------------------+
| id | parent_id | affiliate_id | link_type | linkhash | created_date |
+-----+-----------+--------------+-----------+----------+---------------------+
| 21 | NULL | 3 | PRODUCT | fa2e82a7 | 2011-06-15 16:18:37 |
| 27 | NULL | 3 | PRODUCT | 55de2ae7 | 2011-06-23 01:03:00 |
| 28 | NULL | 3 | PRODUCT | 02cae72f | 2011-06-23 01:03:00 |
| 29 | 27 | 3 | PRODUCT | a4dfb2c8 | 2011-06-23 01:03:00 |
| 30 | 28 | 3 | PRODUCT | 72cea1b2 | 2011-06-23 01:03:00 |
| 36 | 21 | 3 | PRODUCT | fa2e82a7 | 2011-06-23 01:07:03 |
| 59 | 21 | 3 | PRODUCT | ec33413f | 2011-11-04 17:49:17 |
| 60 | 27 | 3 | PRODUCT | f701188c | 2011-11-04 17:49:17 |
| 69 | 21 | 3 | PRODUCT | 6dfb89fd | 2011-11-04 17:49:17 |
+-----+-----------+--------------+-----------+----------+---------------------+
Affiliate Stats
+--------+--------------+--------------------+----------+---------------------+
| id | affiliate_id | link_id | order_id | type | created_date |
+--------+--------------+---------+----------+----------+---------------------+
| 86570 | 3 | 21 | NULL | CLICK | 2013-01-01 00:07:31 |
| 86574 | 3 | 21 | NULL | PAGEVIEW | 2013-01-01 00:08:53 |
| 86579 | 3 | 21 | 411 | SALE | 2013-01-01 00:09:52 |
| 86580 | 3 | 36 | NULL | CLICK | 2013-01-01 00:09:55 |
| 86582 | 3 | 36 | NULL | PAGEVIEW | 2013-01-01 00:09:56 |
| 86583 | 3 | 28 | NULL | CLICK | 2013-01-01 00:11:04 |
| 86584 | 3 | 28 | NULL | PAGEVIEW | 2013-01-01 00:11:04 |
| 86586 | 3 | 30 | NULL | CLICK | 2013-01-01 00:30:18 |
| 86587 | 3 | 30 | NULL | PAGEVIEW | 2013-01-01 00:30:20 |
| 86611 | 3 | 69 | NULL | CLICK | 2013-01-01 00:40:19 |
| 86613 | 3 | 69 | NULL | PAGEVIEW | 2013-01-01 00:40:19 |
| 86619 | 3 | 69 | 413 | SALE | 2013-01-01 00:42:12 |
| 86622 | 3 | 60 | NULL | CLICK | 2013-01-01 00:46:00 |
| 86624 | 3 | 60 | NULL | PAGEVIEW | 2013-01-01 00:46:01 |
| 86641 | 3 | 60 | NULL | PAGEVIEW | 2013-01-01 00:55:58 |
| 86642 | 3 | 30 | 415 | SALE | 2013-01-01 00:56:35 |
| 86643 | 3 | 28 | NULL | PAGEVIEW | 2013-01-01 00:56:43 |
| 86644 | 3 | 60 | 417 | SALE | 2013-01-01 00:56:52 |
+--------+--------------+---------+----------+----------+---------------------+
Orders
+------+--------------+---------+---------------------+
| id | affiliate_id | total | created_date |
+------+--------------+---------+---------------------+
| 411 | 3 | 138.62 | 2013-01-01 00:09:50 |
| 413 | 3 | 312.87 | 2013-01-01 00:09:52 |
| 415 | 3 | 242.59 | 2013-01-01 00:09:55 |
| 417 | 3 | 171.18 | 2013-01-01 00:09:55 |
+------+--------------+---------+---------------------+
Now the results that I need should look like this (only show main/parent link id)
+---------+---------+
| link_id | total |
+---------+---------+
| 21 | 451.49 | <- 1 order from parent (21), 1 from child (69)
| 27 | 171.18 | <- 1 order from child (69)
| 28 | 242.59 | <- 1 order from child (30)
+---------+---------+
I'm not quite sure how to write the query so that I can sum where affiliate_link.id and affiliate_link.parent_id are combined. Is this even possible with a couple of JOINs and GROUPing?
I'm not too sure why you have denormalised affiliate_id (by placing it in each table) and, therefore, whether one can rely on all Stats and Orders that stem from a particular Link to have the same affiliate_id as that Link.
If it's possible, I'd suggest changing the AffiliateLink.parent_id column such that parent records point to themselves (rather than NULL):
UPDATE AffiliateLink SET parent_id = id WHERE parent_id IS NULL
Then it's a simple case of joining and grouping:
SELECT AffiliateLink.parent_id AS link_id,
SUM(Orders.total) AS total
FROM AffiliateLink
JOIN AffiliateStats ON AffiliateStats.link_id = AffiliateLink.id
JOIN Orders ON Orders.id = AffiliateStats.order_id
WHERE AffiliateLink.affiliate_id = 3
GROUP BY AffiliateLink.parent_id
See it on sqlfiddle.
If it's not possible to make the change, you can effectively create the resulting AffiliateLink table using UNION (but beware the performance implications, as MySQL will not be able to use indexes on the result):
(
SELECT parent_id, id, affiliate_id FROM AffiliateLink WHERE parent_id IS NOT NULL
UNION ALL
SELECT id , id, affiliate_id FROM AffiliateLink WHERE parent_id IS NULL
) AS AffiliateLink
See it on sqlfiddle.
I believe all questions of this nature start out this way...
I have two tables: fields and data. Fields describes the column names of a (non-existant) table and data contains the data of that (non-existant) table. Like so...
Fields:
+----+---------+-------------+---------------+-------+----------+
| ID | F_ORDER | NAME | LABEL | VALUE | TYPE |
+----+---------+-------------+---------------+-------+----------+
| 1 | 1 | IS_EMPLOYEE | Region | | checkbox |
| 2 | 3 | EM_AVATAR | Avatar | | avatar |
| 3 | 4 | EM_JOBTITLE | Job Title | | text |
| 4 | 5 | EM_COMPANY | Company | | text |
| 5 | 6 | EM_PHONE | Phone | | text |
| 6 | 2 | EM_ORDER | Display Order | 5 | text |
+----+---------+-------------+---------------+-------+----------+
Data:
+-----+----------+---------+--------------------------------------+
| ID | FIELD_ID | USER_ID | VALUE |
+-----+----------+---------+--------------------------------------+
| 5 | 1 | 1 | YES |
| 6 | 2 | 1 | |
| 7 | 3 | 1 | Owner |
| 8 | 4 | 1 | Acme, Inc. |
| 9 | 5 | 1 | 123-456-7987 |
| 150 | 5 | 31 | 123-623-5555 |
| 149 | 4 | 31 | Acme, Inc. |
| 148 | 3 | 31 | Sales and Customer Support |
| 147 | 2 | 31 | |
| 146 | 1 | 31 | YES |
| 26 | 1 | 6 | NO |
| 27 | 2 | 6 | http://example.com/avi/avi.jpeg |
| 28 | 3 | 6 | CEO |
| 29 | 4 | 6 | Acme |
| 30 | 5 | 6 | (123) 734-5555 |
| 31 | 1 | 7 | NO |
| 32 | 2 | 7 | http://example.com/avi/avi.jpeg |
| 33 | 3 | 7 | VP, Services |
| 34 | 4 | 7 | Acme |
| 35 | 5 | 7 | (913) 963-5555 |
| 36 | 1 | 14 | NO |
| 37 | 2 | 14 | http://example.com/avi/avi.jpeg |
| 38 | 3 | 14 | Senior Accountant |
| 39 | 4 | 14 | Acme |
| 40 | 5 | 14 | (123) 213-5555 |
| 41 | 1 | 10 | NO |
| 42 | 2 | 10 | http://example.com/avi/avi.jpeg |
| 43 | 3 | 10 | President |
| 44 | 4 | 10 | Acme |
| 45 | 5 | 10 | (123) 734-5555 |
| 46 | 1 | 12 | NO |
| 47 | 2 | 12 | http://example.com/avi/avi.jpeg |
| 48 | 3 | 12 | Services Supervisor |
| 49 | 4 | 12 | Acme |
| 50 | 5 | 12 | (123) 573-5555 |
| 51 | 1 | 11 | NO |
| 52 | 2 | 11 | http://example.com/avi/avi.jpeg |
| 53 | 3 | 11 | Operations Supervisor |
| 54 | 4 | 11 | Acme |
| 55 | 5 | 11 | (123) 259-5555 |
| 56 | 1 | 8 | NO |
| 57 | 2 | 8 | http://example.com/avi/avi.jpeg |
| 58 | 3 | 8 | General Information |
| 59 | 4 | 8 | Acme |
| 60 | 5 | 8 | (123) 213-5555 |
| 61 | 1 | 9 | NO |
| 62 | 2 | 9 | http://example.com/avi/avi.jpeg |
| 63 | 3 | 9 | VP, Sales |
| 64 | 4 | 9 | Acme |
| 65 | 5 | 9 | (123) 210-5555 |
+-----+----------+---------+--------------------------------------+
The basic verbiage of the query I'm looking for is: I want all information for all people who are employees (IS_EMPLOYEE = "YES") and ordered by their display order column (EM_ORDER).
My query so far gets me nowhere. I get results a little like this:
+--------+------------+------------+-------+---------+-------+
ID FIELD_ID NAME LABEL TYPE VALUE
+--------+------------+------------+-------+---------+-------+
7 1 IS_EMPLOYEE Region checkbox YES
+--------+------------+------------+-------+---------+-------+
What I need are results like this:
+-------+------------+---------+-----------+----------+-------------+--------+
USER_ID IS_EMPLOYEE EM_AVATAR EM_JOBTITLE EM_COMPANY EM_PHONE EM_ORDER
+-------+------------+---------+-----------+----------+-------------+--------+
6 YES http:// CEO Acme 123-123-555 5
+-------+------------+---------+-----------+----------+-------------+--------+
And of course I'm trying to get it all back into PHP as a usable array ($results['user_id']['jobtitle'] etc.). I could just get everything and work through it with PHP, but I'm trying to learn MySQL and I think this is a faster method than doing several foreach blocks ... although I could be wrong.
Thanks in advance for any help.
Your data table is organized in the Entity-Attribute-Value manner, which is not a valid way of storing relational data. It's no wonder that it's awkward to retrieve it with SQL into a conventional row with one column per attribute.
To do this with SQL, you basically need to do a pivot query. This is hard to write and terribly inefficient to execute.
You're better off fetching all the data for employees:
SELECT e.*, f.label
FROM Data AS is_employee
JOIN Data AS e USING (user_id)
JOIN Fields AS f ON e.field_id = f.id
WHERE (is_employee.field_id, is_employee.value) = (1, 'Yes');
And then reorganizing it into a collection of associative arrays in PHP code:
while ($row = $stmt->fetch()) {
$data[ $row['user_id'] ][ $row['label'] ] = $row['value'];
}
This is basically a PIVOT but MySQL does not have a PIVOT function so you can replicate it using an aggregate function and a CASE statement.
A Static version is when you know all of the values that you want to transform into columns (these are your field names):
select
d.user_id,
max(case when f.name = 'IS_EMPLOYEE' then d.value else null end) IS_EMPLOYEE,
max(case when f.name = 'EM_AVATAR' then d.value else null end) EM_AVATAR,
max(case when f.name = 'EM_JOBTITLE' then d.value else null end) EM_JOBTITLE,
max(case when f.name = 'EM_COMPANY' then d.value else null end) EM_COMPANY,
max(case when f.name = 'EM_PHONE' then d.value else null end) EM_PHONE,
max(case when f.name = 'EM_ORDER' then d.value else null end) EM_ORDER
from data d
left join fields f
on f.id = d.FIELD_ID
group by d.user_id
See SQL Fiddle with Demo
Result:
| USER_ID | IS_EMPLOYEE | EM_AVATAR | EM_JOBTITLE | EM_COMPANY | EM_PHONE | EM_ORDER |
---------------------------------------------------------------------------------------------------------------------------------
| 1 | YES | (null) | Owner | Acme, Inc. | 123-456-7987 | (null) |
| 6 | NO | http://example.com/avi/avi.jpeg | CEO | Acme | (123) 734-5555 | (null) |
| 7 | NO | http://example.com/avi/avi.jpeg | VP, Services | Acme | (913) 963-5555 | (null) |
| 8 | NO | http://example.com/avi/avi.jpeg | General Information | Acme | (123) 213-5555 | (null) |
| 9 | NO | http://example.com/avi/avi.jpeg | VP, Sales | Acme | (123) 210-5555 | (null) |
| 10 | NO | http://example.com/avi/avi.jpeg | President | Acme | (123) 734-5555 | (null) |
| 11 | NO | http://example.com/avi/avi.jpeg | Operations Supervisor | Acme | (123) 259-5555 | (null) |
| 12 | NO | http://example.com/avi/avi.jpeg | Services Supervisor | Acme | (123) 573-5555 | (null) |
| 14 | NO | http://example.com/avi/avi.jpeg | Senior Accountant | Acme | (123) 213-5555 | (null) |
| 31 | YES | (null) | Sales and Customer Support | Acme, Inc. | 123-623-5555 | (null) |
If you have an unknown number of values to turn into columns, then you can use a prepared statement to generate the sql dynamically.
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when f.name = ''',
name,
''' then d.value end) AS ',
name
)
) INTO #sql
FROM fields;
SET #sql = CONCAT('SELECT d.user_id, ', #sql, '
from data d
left join fields f
on f.id = d.FIELD_ID
GROUP BY d.user_id');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo
Both will produce the same result.
You can then add a WHERE clause to filter out any of the unneeded rows.