I've got a complex (For me!) query that basically is grabbing the data from 2 tables then grouping them togeher (This bit works 100% as a SELECT query)
But when I now need to update the database in another table, it won't work.
I've got this:
UPDATE
exp_channel_data data,
(
SELECT
posts.cat_id,
posts.entry_id,
cats.cat_name,
cats.cat_id,
GROUP_CONCAT('{"',cats.cat_name, '"}:{"',cats.cat_name,'"}') as category_tag
FROM
exp_category_posts posts,
exp_categories cats
WHERE
cats.cat_id = posts.cat_id
GROUP BY
posts.entry_id
) category
SET
data.field_id_178 = category.category_tag
WHERE
data.entry_id = category.entry_id;
But I'm getting this error:
Duplicate column name 'cat_id'
I think its because i'm trying to make the connection between the two tables, BUT it's not linking.
Like I said, the SELECT query works on it's own, but when put in to the UPDATE - It just throws this error.
:(
You are very close to what you need:
UPDATE exp_channel_data data JOIN
(SELECT posts.entry_id,
GROUP_CONCAT('{"',cats.cat_name, '"}:{"',cats.cat_name,'"}') as category_tag
FROM exp_category_posts posts JOIN
exp_categories cats
ON cats.cat_id = posts.cat_id
GROUP BY posts.entry_id
) category
ON data.entry_id = category.entry_id
SET data.field_id_178 = category.category_tag;
The important part was removing cats.cat_id from the subquery. You had two columns with that name, confusing MySQL.
I also fixed the query to use proper, explicit JOIN syntax.
Related
I have a problem with this mysql query. When I run it in phpmyadmin it shows only the "player_11" and the "player_11Count" . I can't join the players table. There are no errors shown. It acts just like the JOIN is not there. Do you have any ideas?
SELECT player_11, COUNT(player_11) AS player_11Count
FROM user_teams
LEFT JOIN players ON user_teams.player_11 = players.player_id
WHERE round_id = '31' && user_teams.team_id = '22'
GROUP BY player_11
ORDER BY COUNT(player_11) DESC
You are saying to mysql "show me only player_11 and count(player_11)". You can't have anything else even with joining tables. Joining tables doesn't automatically select fields (unless your are using SELECT *)
You should say:
SELECT players.*, user_teams.player_11, COUNT(user_teams.player_11) AS player_11Count
FROM user_teams .....
Then your players data will show up
I'm really struggling with this query and I hope somebody can help.
I am querying across multiple tables to get the dataset that I require. The following query is an anonymised version:
SELECT main_table.id,
sub_table_1.field_1,
main_table.field_1,
main_table.field_2,
main_table.field_3,
main_table.field_4,
main_table.field_5,
main_table.field_6,
main_table.field_7,
sub_table_2.field_1,
sub_table_2.field_2,
sub_table_2.field_3,
sub_table_3.field_1,
sub_table_4.field_1,
sub_table_4.field_2
FROM main_table
INNER JOIN sub_table_4 ON sub_table_4.id = main_table.id
INNER JOIN sub_table_2 ON sub_table_2.id = main_table.id
INNER JOIN sub_table_3 ON sub_table_3.id = main_table.id
INNER JOIN sub_table_1 ON sub_table_1.id = main_table.id
WHERE sub_table_4.field_1 = '' AND sub_table_4.field_2 = '0' AND sub_table_2.field_1 != ''
The query works, the problem I have is sub_table_1 has a revision number (int 11). Currently I get duplicate records with different revision numbers and different versions of sub_table_1.field_1 which is to be expected, but I want to limit the result set to only include results limited by the latest revision number, giving me only the latest sub_table_1_field_1 and I really can not figure it out!
Can anybody lend me a hand?
Many Thanks.
It's always important to remember that a JOIN can be on a subquery as well as a table. You could build a subquery that returns the results you want to see then, once you've got the data you want, join it in the parent query.
It's hard to 'tailor' an answer that's specific to you problem, as it's too obfuscated (as you admit) to know what the data and tables really look like, but as an example:
Say table1 has four fields: id, revision_no, name and stuff. You want to return a distinct list of name values, with their latest version of stuff (which, we'll pretend varies by revision). You could do this in isolation as:
select t.* from table1 t
inner join
(SELECT name, max(revision_no) maxr
FROM table1
GROUP BY name) mx
on mx.name = t.name
and mx.maxr = t.revision_no;
(Note: see fiddle at the end)
That would return each individual name with the latest revision of stuff.
Once you've got that nailed down, you could then swap out
INNER JOIN sub_table_1 ON sub_table_1.id = main_table.id
....with....
INNER JOIN (select t.* from table1 t
inner join
(SELECT name, max(revision_no) maxr
FROM table1
GROUP BY name) mx
on mx.name = t.name
and mx.maxr = t.revision_no) sub_table_1
ON sub_table_1.id = main_table.id
...which would allow a join with a recordset that is more tailored to that which you want to join (again, don't get hung up on the actual query I've used, it's just there to demonstrate the method).
There may well be more elegant ways to achieve this, but it's sometimes good to start with a simple solution that's easier to replicate, then simplify it once you've got the general understanding of the what and why nailed down.
Hope that helps - as I say, it's as specific as I could offer without having an idea of the real data you're using.
(for the sake of reference, here is a fiddle with a working version of the above example query)
In your case where you only need one column from the table, make this a subquery in your select clause instead of than a join. You get the latest revision by ordering by revision number descending and limiting the result to one row.
SELECT
main_table.id,
(
select sub_table_1.field_1
from sub_table_1
where sub_table_1.id = main_table.id
order by revision_number desc
limit 1
) as sub_table_1_field_1,
main_table.field_1,
...
FROM main_table
INNER JOIN sub_table_4 ON sub_table_4.id = main_table.id
INNER JOIN sub_table_2 ON sub_table_2.id = main_table.id
INNER JOIN sub_table_3 ON sub_table_3.id = main_table.id
WHERE sub_table_4.field_1 = ''
AND sub_table_4.field_2 = '0'
AND sub_table_2.field_1 != '';
I am working on a query with the following format:
I require all the columns from the Database 'A', while I only require the summed amount (sum(amount)) from the Database 'B'.
SELECT A.*, sum(B.CURTRXAM) as 'Current Transaction Amt'
FROM A
LEFT JOIN C
ON A.Schedule_Number = C.Schedule_Number
LEFT JOIN B
ON A.DOCNUMBR = B.DOCNUMBR
ON A.CUSTNMBR = B.CUSTNMBR
GROUP BY A
ORDER BY A.CUSTNMBR
My question is regarding the grouping statement, database A has about 12 columns and to group by each individually is tedious, is there a cleaner way to do this such as:
GROUP BY A
I am not sure if a simpler way exists as I am new to SQL, I have previously investigated GROUPING_ID statements but thats about it.
Any help on lumped methods of grouping would be helpful
Since the docnumber is the primary key - just use the following SQL:
SELECT A.*, sum(B.CURTRXAM) as 'Current Transaction Amt'
FROM A
LEFT JOIN C
ON A.Schedule_Number = C.Schedule_Number
LEFT JOIN B
ON A.DOCNUMBR = B.DOCNUMBR
ORDER BY RM20401.CUSTNMBR
GROUP BY A.DOCNUMBR
I have a SQL query that looks like this:
DELETE
price.*
FROM
price
JOIN
service
ON
price.service_id = service.id
WHERE
price.country_from_id NOT IN
(SELECT
country_id
FROM
carrier_zone_country
JOIN
carrier_zone
ON
carrier_zone_id = carrier_zone.id
WHERE
carrier_zone.carrier_service_id = service.carrier_service_id)
OR
price.country_to_id NOT IN
(SELECT
country_id
FROM
carrier_zone_country
JOIN
carrier_zone
ON
carrier_zone_id = carrier_zone.id
WHERE
carrier_zone.carrier_service_id = service.carrier_service_id)
I was hoping to avoid running the subquery twice by moving it into the FROM clause and giving it a name. However, that gave me syntax errors. Looking at the documentation, I can see that only the SELECT FROM clause can have a named subquery in it.
Firstly I am wondering why that is the case? And secondly, how could I re-write this SQL query to avoid performing the same subquery twice.
Do one single NOT EXISTS sub-query, where both to and from countries are checked:
DELETE
price.*
FROM
price
JOIN
service
ON
price.service_id = service.id
WHERE
NOT EXIST(SELECT
1
FROM
carrier_zone_country
JOIN
carrier_zone
ON
carrier_zone_id = carrier_zone.id
WHERE country_id IN (price.country_from_id, price.country_to_id)
AND carrier_zone.carrier_service_id = service.carrier_service_id))
So after helpful feedback from my original question, I now have this query:
SELECT sessions.id, sessions.title, sessions.abstract, sessions.presenters, sessions.proposal_id, proposals.outcomes, proposals.CategorySelection, proposals.research3, proposals.research4, proposals.research5, proposals.research6, proposals.innovation3, proposals.innovation4, proposals.innovation5,proposals.innovation6, proposals.application3, proposals.application4, proposals.application5, proposals.application6, proposals.integration3, proposals.integration4, proposals.integration5, proposals.integration6, proposals.references, proposals.organization
FROM sessions, proposals
INNER JOIN proposals ON proposals.id = sessions.proposal_id
WHERE sessions.id = '$id
LIMIT 1;)
that is getting me nowhere fast. What am I doing wrong?
Original question:
I need to pull several fields from one table and several more from a second table. The criteria is that a field called proposal_id match the id field of the second table. I am fairly new so this is what I have so far. It is not working, but not sure how to make it work.
(SELECT `title`,`abstract`,`presenters`,`proposal_id` FROM `sessions` WHERE `id`='$id')
UNION
(SELECT `outcomes`,`CategorySelection`,`research3`,`research4`,`research5`,`research6`,`innovation3`,`innovation4`,`innovation5`,
`innovation6`,`application3`,`application4`,`application5`,`application6`,`integration3`,`integration4`,`integration5`,`integration6`,`references`,`organization` FROM `proposals` WHERE `id`= `sessions`.`proposal_id`)
LIMIT 1;
You need to use JOIN not UNION
select
s.*,p.*
from `sessions` s
inner join `proposals` p on p.id = s.proposal_id
where s.id = '$id'
This is how you can join both the tables using the common key between.
You can select the specific fields instead of .* by specifying the column names as
s.col1,s.col2,p.col1,p.col2
etc
Try to use JOINS, where you can match the related fields from both the tables , this is the most convenient way to fetch records from multiple tables
UNION is used when you want to combine two queries
select a.id,b.some_field from table1 as a
INNER JOIN table2 as b ON b.prospal_id = a.id