I am having some problems figuring out the best use of GROUP_CONCAT. In the query below I am trying to select multiple images from JAM_Plot_Images and set them against single records. So get record 1, display image1.jpg, image2.jpg in each row. Then get record 2, display image3.jpg, image4.jpg in the 2nd row and so on.
SELECT *, GROUP_CONCAT(JAM_Plot_Images.image ORDER BY JAM_Plot_Images.image) AS images
FROM JAM_Plots
LEFT JOIN JAM_Plot_Images ON JAM_Plots.page_id = JAM_Plot_Images.page_id
GROUP BY JAM_Plots.page_id
The problem I have is if a row has no images in a row it breaks the unique identifier when outputting the records, but only for that record. So if records 1 2 and 4 have images it will output everything fine, but if record 3 has no image the unique ID won't appear. NULL values appear within phpmyadmin output. I have tried to use COALESCE to fix the issue but can't quite get it to work.
This problem has nothing to do with GROUP_CONCAT(), it's a general problem with LEFT JOIN when you have duplicate column names between tables.
Since you have the same column name page_id in both tables, both of them will be selected in the results. But when there's no images, JAM_Plot_Images.page_id will be NULL.
To disambiguate them, you should give an alias to JAM_Plots.page_id, and use that.
SELECT *, JAM_Plots.page_id AS jp_page_id, GROUP_CONCAT(...)
...
Related
I have two tables called tc_revenue and tc_rates.
tc_revenue contains :- code, revenue, startDate, endDate
tc_rate contains :- code, tier, payout, startDate, endDate
Now I need to get records where code = 100 and records should be unique..
I have used this query
SELECT *
FROM task_code_rates
LEFT JOIN task_code_revenue ON task_code_revenue.code = task_code_rates.code
WHERE task_code_rates.code = 105;
But I am getting repeated records help me to find the correct solution.
eg:
in this example every record is repeated 2 time
Thanks
Use a group by for whatever field you need unique. For example, if you want one row per code, then:
SELECT * FROM task_code_rates LEFT JOIN task_code_revenue ON task_code_revenue.code = task_code_rates.code
where task_code_rates.code = 105
group by task_code_revenue.code, task_code_revenue.tier
If code admits duplicates in both tables and you perform join only using code, then you will get the cartessian product between all matching rows from one table and all matching rows from the other.
If you have 5 records with code 100 in first table and 2 records with code 100 in second table, you'll get 5 times 2 results, all combinations between matching rows from the left and the right.
Unless you have duplicates inside one (or both) tables, all 10 results will differ in colums coming either from one table, the other or both.
But if you were expecting to get two combined rows and three rows from first table with nulls for second table columns, this will not happen.
This is how joins work, and anyway, how should the database decide which rows to combine if it didn't generate all combinations and let you decide in where clause?
Maybe you need to add more criteria to the ON clause, such as also matching dates?
Hello I'm rather new to sql and I want to ask if there is any way to get my whole table of users listed with an extra column being the first image for each one, e.g. if one users have 5 images, instead of returning 5 rows with repeated values for user and diffrerent images I want to return only one so there is no repeated value.
The SQL SELECT DISTINCT Statement
In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different) values.
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name,column_name
FROM table_name;
As the title suggests I have a MySQL query like this:
SELECT DISTINCT `friendly_url` FROM `post` WHERE `description` LIKE ? OR `heading` LIKE ? ORDER BY `friendly_url`
I have given the string '%' wild card in the parameters so that this works as a Search function. How ever, Say a user is searching for a common word like 'is' and it appears in both heading and description in the same post. Then this query returns the same post twice. I don't want that to happen, hence the 'DISTINCT'.
Why does this happen? Any way I can work around to make it work the way i want?
The query is not returning the same row twice. The predicates in the WHERE clause are evaulated against each row, and either the row is returned, or it's not. No combination of OR conditions is going to cause MySQL to return the "same row" multiple times.
If you are getting duplicate values of friendly_url, then you have multiple rows in the post table that have the same value for friendly_url. (i.e. friendly_url column is not unique in the post table.)
You don't have to use the DISTINCT keyword to remove duplicates from the resultset. You could remove the DISTINCT keyword, and add GROUP BY friendly_url before the ORDER BY clause.
To identify the "duplicated" values of friendly_url, and how many rows have that same value:
SELECT p.friendly_url
, COUNT(1)
FROM post p
GROUP BY p.friendly_url
HAVING COUNT(1) > 1
Is this supposed to count the appearances of each link_id on the table?
SELECT link_id, count(*) FROM table group by link_id
I think it should, but if I just execute
SELECT * FROM table
I get different results. For example, for link 7 I get a count of 40 in the first query, but using 'select *' i see that there are only 4 rows of link 7... What's going on?
Yes it is supposed to do that,
Surely it would be easier to do a
SELECT DISTINCT count(link_id) FROM table
This would give you a single row containing the amount of link_id's
Alternatively
SELECT link_id,count(*) FROM table GROUP BY link_id's
Returns multiple rows containing the count of each
With regard to the original question you mention there are multiple rows per id, are you doing a join any where?
I get different results. For example, for link 7 I get a count of 40 in the first query, but using 'select *' i see that there are only 4 rows of link 7... What's going on?
Are you sure phpMyAdmin or similar isn't limiting the amount of rows you are seeing?
I've got a MySQL database that stores image information. Right now it only has three rows stored in the database, and each row is associated with something like, for instance, a unique blog post via a key column.
As of right now, one "blog post key" has one image, and one has two images in the database.
When I run this query, MySQL returns all three rows.
SELECT `id`, `key`, `url`
FROM (`images`)
WHERE `key` = 'TpaS4G5h'
OR `key` = '78855e44'
However, when i add the GROUP BY statement I only get two rows... one for each key.
SELECT `id`, `key`, `url`
FROM (`images`)
WHERE `key` = 'TpaS4G5h'
OR `key` = '78855e44'
GROUP BY `key`
I'm sure there is a simple solution, but I don't know what it is... so any help would be really appreciated.
Thanks in advance!
GROUP BY groups all rows with the group by value into a single row, so it's doing exactly what it's supposed to. If you want the rows with the same key to be returned in consecutive rows, do an ORDER BY instead.
You don't need a group by for this query! Group by groups identical values for the selected column into a single row.
You are specifying two keys in the first query and returning three rows which means you have at list one duplicate in your key column. To validate this if you do a
select distinct key from images
you should only see two rows. This means if you group by this column all results that have this key in common will be rolled up in the result explaining the behavior you are seeing.
GROUP BY is used to "collapse" any rows which match the grouping criteria into a single row, so you can use the aggregate functions on them. SUM(), COUNT(), etc..
If you mean you want to group the records so that you that you can retrieve all the images associated with each key in a single fetchrow call, well, that's not what GROUP BY is for. You're trying to convert multi-row based data sets into a single row. If all you were trying to do was retrieve the IDs of those images, and do it in a single fetch per row, you could use something like
SELECT key, group_concat(id)
FROM images
WHERE key=XXX or key=YYY
GROUP BY key
That would give you a data set that looks like this:
key group_concat(id)
------------------------
xxx 100,101
yyy 102,103
but that would require post-processing in your script to seperate those concat'd IDs into seperate numbers.