I have a database with 4 tables related to this question. I have only provided the required columns for each table to not get things complicated.
reviews
ID | reviewID
themes
ID | themeID | name
reviewthemes
ID | reviewID | themeID
reviewsubthemes
ID | reviewID | themeID
So a review can have multiple themes and multiple sub-themes. Each sub-theme should correspond to a theme, meaning a sub-theme cannot exist without its main theme.
The themes table is the place which holds the name for either the theme or sub-theme.
At the minute i have made a query which grabs all the data i require. Because it makes multiple rows i have done a group_concat for the themes and sub-themes. This means for each individual review i have a column called "themes". In this column it contains all the themes and sub-themes separated by a comma.
This is my query:
select `reviews`.`reviewID`,
GROUP_CONCAT(`themes`.`name`) as `Name`
from `reviews`
left join `reviewthemes` on `reviewthemes`.`reviewId` =`reviews`.`reviewId`
left join `reviewsubthemes` on `reviewsubthemes`.`reviewid` = `reviews`.`reviewid`
left join `themes` on (`themes`.`themeid` = `reviewthemes`.`themeid` or `themes`.`themeid` = `reviewsubthemes`.`themeid`)
where `reviews`.`healthwatchID` = 32
and (`review_created` BETWEEN '2015-08-01 00:00:00' AND '2015-08-31 23:59:59')
group by `reviews`.`reviewID`
This brings back data which looks like this:
Now here is the tricky part, and i'm not sure if i will need to do this in PHP or MYSQl but its safe to say i'm am really confused.
I require the format to be like :
theme - subtheme; theme - subtheme; theme;
So you can see i need a theme and a subtheme to have a semi colon at the end. But some reviews can have a theme with no sub-theme followed by a theme with a sub-theme.
What is the best way to go about this?
You can use CONCAT to join the name and sub name together, and then GROUP_CONCAT on the results of that:-
select reviews.reviewID,
GROUP_CONCAT(CONCAT_WS(' - ', t1.name, t2.name) SEPARATOR ';') as Name
from reviews
left join reviewthemes on reviewthemes.reviewId =reviews.reviewId
left join reviewsubthemes on reviewsubthemes.reviewid = reviews.reviewid
left join themes t1 on (t1.themeid = reviewthemes.themeid)
left join themes t2 on (t2.themeid = reviewsubthemes.themeid)
where reviews.healthwatchID = 32
and (review_created BETWEEN '2015-08-01 00:00:00' AND '2015-08-31 23:59:59')
group by reviews.reviewID
Related
This question already has answers here:
How to resolve ambiguous column names when retrieving results?
(11 answers)
Closed 2 years ago.
I have some big tables which I need to combine into a single very large table, to form a single-page data export for a statistical package.
This is easy with INNER JOIN but the some of the tables have the same column names and these are being overwritten by each other when I fetch them as an array in PHP.
There are 4 tables being joined with 30-200 columns in each so there are far too many field names to manually include in the query with aliases, as would be the norm in this situation.
Here's the query:
SELECT * FROM logs
INNER JOIN logdetail ON logdetail.logID = logs.id
INNER JOIN clients ON clients.id = logs.clientID
INNER JOIN records ON records.id = logdetail.id
WHERE logs.userID=1
Is there any way around this? I don't actually mind what the column names are as long as I have the data so if I could prepend the table name to each field, that would do the trick.
I would create a view, your view would be comprised of your long query with aliases
Here is an example taken from the manual
mysql> CREATE TABLE t (qty INT, price INT);
mysql> INSERT INTO t VALUES(3, 50);
mysql> CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;
mysql> SELECT * FROM v;
+------+-------+-------+
| qty | price | value |
+------+-------+-------+
| 3 | 50 | 150 |
+------+-------+-------+
This has always worked for me, unless you have one to many or some other relationship among these tables, which will duplicate records.
SELECT * FROM logs l
INNER JOIN logdetail ld ON ld.logID = l.id
INNER JOIN clients c ON c.id = l.clientID
INNER JOIN records r ON r.id = ld.id
WHERE l.userID=1
As andrew says you can also use a View to get this thing working which is much cooler.
I found a solution for this. Simply, fetch each duplicate column a second time, this time using an alias. This way, the overwritten values are selected again and aliased:
SELECT * FROM logs,
clients.name as clientName,
logs.name as logName,
etc...
INNER JOIN logdetail ON logdetail.logID = logs.id
INNER JOIN clients ON clients.id = logs.clientID
INNER JOIN records ON records.id = logdetail.id
WHERE logs.userID=1
Note: There is no need to do this for the final instance of the duplicate, because this column will not have been overwritten. So, in the example above, there is no need to include a line like records.name as recordName because, since there are no columns after it which have the same name, the record.name field was never overwritten and is already available in the name column.
Table structure
client_commands (the "main" table):
id | completed
command_countries:
id | command_id | country_code
command_os:
id | command_id |OS
command_id on references the id column on client_commands.
Problem
I can add client commands with filters based on countries and operating systems. To try and normalise my DB structure, for each new command added:
Add a new row to client_commands
For each country, I add a new row to command_countries, each referencing client_command.id
For each OS, I add a new row to command_os, each referencing client_command.id
For one of the pages on my site, I need to display all client_commands (where completed = 0) as well as all the countries and operating systems for that command. My desired output would be something like:
id | countries | OS
1 | GB, US, FR| 2, 3
2 | ES, HU | 1, 3
I'm not sure how to go about doing this. The current query I'm using returns multiple rows:
SELECT a.id, b.country_code, c.OS
FROM client_commands a
LEFT JOIN command_countries b on b.command_id = a.id
LEFT JOIN command_os c on c.command_id = a.id
WHERE a.completed = 0
Any help?
Thanks!
EDIT: I forgot to mention (if you couldn't infer from above) - there can be a different number of operating systems and countries per command.
--
Also: I know I could do this by pulling all the commands, then looping through and running 2 additional queries for each result. But if I can, I'd like to do it as efficiently as possible with one query.
You can do this in one query by using GROUP_CONCAT
SELECT a.id,
GROUP_CONCAT(DISTINCT b.country_code SEPARATOR ' ,') `countries`,
GROUP_CONCAT(DISTINCT c.OS SEPARATOR ' ,') `os`,
FROM client_commands a
LEFT JOIN command_countries b on b.command_id = a.id
LEFT JOIN command_os c on c.command_id = a.id
WHERE a.completed = 0
GROUP BY a.id
if you want the ordered results in in a row you can use ORDER BY in GROUP_CONCAT like
GROUP_CONCAT(b.country_code ORDER BY b.command_id DESC SEPARATOR ' ,') `countries`
But be aware of that fact it has a limit of 1024 character to concat set by default but this can be increased b,steps provided in manual
So I have a mysql database with three tables that has three table I am trying to retrieve rows of data from content based on a condition on data phpro_tag_types
The structures of the tables is like so
phpro_tag_types
tag_type_id | tag_type_name
<pk>
phpro_tag_targets
tag_target_id | tag_id | sub_tag_id | tag_target_name | tag_type_id
<pk> | <FK> | <FK> | | <FK>
content
content_id | tag_target_id | bunch of other things|
<pk> | <fk> |
The relationships between the table is like so
content.tag_target_id : phpro_tag_targets.tag_target_id
1 : m //each tag_target_id is in content once
//and phpro_tag_targets many times
phpro_tag_targets.tag_type_id : phpro_tag_types.tag_type_id
M: 1 // there is many occurrences of tag_type_id
//in phpro_tag_targets and one occurrence in of tag_type_id in phpro_tag_type
(I hope I have explained this thoroughly enough using the correct terms if not I apologize, clearly I am still kind of green with this)
Now I have a SQL query that looks like this
SELECT *
FROM phpro_tag_types types
INNER JOIN phpro_tag_targets targets ON types.tag_type_id=targets.tag_type_id
INNER JOIN content c ON targets.tag_target_id = c.tag_target_id
WHERE types.tag_type_id=14
ORDER BY update_time DESC
Now this query works however not exactly quite as I intended. The problem is the resulting array that is returned has multiple instance of the same piece of content ie. a single content_id (I believe because the same tag_target_id exists in phpro_tag_targets multiple times) however I would only like the results array to only contain unique content_id's as this is the data I am actually outputting to users.
As a side note putting Distinct into the query also doesn't seem to work as there is no way to only make sure content is DISTINCT (at least I could find)
Any help with this would be greatly appreciated as I am kind of lost on how to achieve this
"I am trying to retrieve rows of data from content based on a condition on data phpro_tag_types"
Assuming you are trying to fetch fields of content. What about following IN() sub-query
SELECT *
FROM content c
WHERE tag_target_id IN (
SELECT DISTINCT tag_target_id
FROM phpro_tag_types types
INNER JOIN phpro_tag_targets targets ON types.tag_type_id=targets.tag_type_id
WHERE types.tag_type_id=14
)
ORDER BY update_time DESC;
BTW,
"As a side note putting Distinct into the query also doesn't seem to work as there is no way to only make sure content is DISTINCT (at least I could find)"
DISTINCT c.* does not make sense?
SELECT DISTINCT c.*
FROM phpro_tag_types types
INNER JOIN phpro_tag_targets targets ON types.tag_type_id=targets.tag_type_id
INNER JOIN content c ON targets.tag_target_id = c.tag_target_id
WHERE types.tag_type_id=14
ORDER BY update_time DESC;
There's a general trick to do a left outer join on the duplicating table in such a way that only one entry has null values and then limit the query to that row:
SELECT *
FROM phpro_tag_types types
INNER JOIN phpro_tag_targets targets ON types.tag_type_id=targets.tag_type_id
INNER JOIN content c ON targets.tag_target_id = c.tag_target_id
LEFT OUTER JOIN phpro_tag_targets t2 on targets.tag_target_id=t2.tag_target_id
AND t2.tag_id < targets.tag_id
WHERE types.tag_type_id=1
AND t2.tag_target_id IS NULL;
This seems a bit crazy but does work and in my experience is performant unless you're dealing with a 'very large' data set (whatever that means).
I'm not 100% sure what the semantics here are, but this query is assuming that you don't care what 'tag_id' you get, you just want to get any tag that matches the 'type_id', which looks to me to match your intent.
NOTE: this will cause duplicate column names in the '*' select, so you need to limit to types.*, targets.*, content.* or spell out the column names.
I have two tables images2 and image_data
So the goal is to have 1 table for ALL the image uploads (images2) and then the image_data is to assign that image with different options.
So for example, here is some sample data:
So image_id 10 has more than one row, because this image is associated with both TN and CQ. And they could also be associated with more than one language and slide, so I would add on to the second row and change slide_id or language_id .. if wanted to add more, than I would add a new row for it.
The goal is to have a list of all the images, and then when you click on it it pops up and you can edit the options and change it straight from there.
I need help writing a query. The one I have right now:
SELECT images2.id, images2.filename, image_data.slide_id, image_data.language_id,
image_data.type FROM images2
LEFT JOIN image_data ON images2.id = image_data.image_id
A couple things wrong with that query.. It is showing the duplicates, because image_id 10 has two rows.
But I need that second row of image #10 because I need to see that it is also associated with CQ so I can check the checkbox when it pops up.
So I need to create a query to show ALL the unique images no duplicates, with all of the options it has.
I'm not sure the best way to do this.. do I need to re-do the way my database tables are? Any help is appreciated.
Thanks.
What you could do is use GROUP_CONCAT() to turn values in multiple rows into a single concatenated string. The following retrieves the ids of slides and languages as well as their names to better facilitate your form.
SELECT
a.id,
a.filename,
GROUP_CONCAT(CONCAT(b.slide_id, '::', c.slide_name)) AS slides,
GROUP_CONCAT(CONCAT(b.language_id, '::', d.language_name)) AS languages,
GROUP_CONCAT(b.type) AS types,
FROM
images a
LEFT JOIN
image_data b ON a.id = b.image_id
LEFT JOIN
slides c ON b.slide_id = c.id
LEFT JOIN
languages d ON c.language_id = d.id
GROUP BY
a.id
Your result set for image 10 will look something like:
id | image_filename | slides | languages | types
-------------------------------------------------------------------------
10 | p170sfhe... | 5::slide5 | 1::language1 | TN,CQ
In php, just explode() the strings based on the delimiters.
you could use GROUP_CONCAT to get a csv of the fields:
SELECT
images2.id,
images2.filename,
GROUP_CONCAT(DISTINCT image_data.slide_id) AS slides,
GROUP_CONCAT(DISTINCT image_data.language_id) AS langs,
GROUP_CONCAT(DISTINCT image_data.type) AS types
FROM images2
LEFT JOIN image_data ON (images2.id = image_data.image_id)
GROUP BY images2.id
I have the following database structure:
Sites table
id | name | other_fields
Backups table
id | site_id | initiated_on(unix timestamp) | size(float) | status
So Backups table have a Many to One relationship with Sites table connected via site_id
And I would like to output the data in the following format
name | Latest initiated_on | status of the latest initiated_on row
And I have the following SQL query
SELECT *, `sites`.`id` as sid, SUM(`backups`.`size`) AS size
FROM (`sites`)
LEFT JOIN `backups` ON `sites`.`id` = `backups`.`site_id`
WHERE `sites`.`id` = '1'
GROUP BY `sites`.`id`
ORDER BY `backups`.`initiated_on` desc
The thing is, with the above query I can achieve what I am looking for, but the only problem is I don't get the latest initiated_on values.
So if I had 3 rows in backups with site_id=1, the query does not pick out the row with the highest value in initiated_on. It just picks out any row.
Please help, and
thanks in advance.
You should try:
SELECT sites.name, FROM_UNIXTIME(b.latest) as latest, b.size, b.status
FROM sites
LEFT JOIN
( SELECT bg.site_id, bg.latest, bg.sizesum AS size, bu.status
FROM
( SELECT site_id, MAX(initiated_on) as latest, SUM(size) as sizesum
FROM backups
GROUP BY site_id ) bg
JOIN backups bu
ON bu.initiated_on = bg.latest AND bu.site_id = bg.site_id
) b
ON sites.id = b.site_id
In the GROUP BY subquery - bg here, the only columns you can use for SELECT are columns that are either aggregated by a function or listed in the GROUP BY part.
http://dev.mysql.com/doc/refman/5.5/en/group-by-hidden-columns.html
Once you have all the aggregate values you need to join the result again to backups to find other values for the row with latest timestamp - b.
Finally join the result to the sites table to get names - or left join if you want to list all sites, even without a backup.
Try with this:
select S.name, B.initiated_on, B.status
from sites as S left join backups as B on S.id = B.site_id
where B.initiated_on =
(select max(initiated_on)
from backups
where site_id = S.id)
To get the latest time, you need to make a subquery like this:
SELECT sites.id as sid,
SUM(backups.size) AS size
latest.time AS latesttime
FROM sites AS sites
LEFT JOIN (SELECT site_id,
MAX(initiated_on) AS time
FROM backups
GROUP BY site_id) AS latest
ON latest.site_id = sites.id
LEFT JOIN backups
ON sites.id = backups.site_id
WHERE sites.id = 1
GROUP BY sites.id
ORDER BY backups.initiated_on desc
I have removed the SELECT * as this will only work using MySQL and is generally bad practice anyway. Non-MySQL RDBSs will throw an error if you include the other fields, even individually and you will need to make this query itself into a subquery and then do an INNER JOIN to the sites table to get the rest of the fields. This is because they will be trying to add all of them into the GROUP BY statement and this fails (or is at least very slow) if you have long text fields.