Guys was looking for a little help. I was trying to run this query in php myadmin. Needing to update a temp table with 4 sets of data..3 sets from one table..and 1 from another table, while joining 3 tables. The error comes back after my second JOIN that I do not have a unique table alias. I'm not sure how to fix that. Can anyone see where the query is going wrong? Thanks.
INSERT INTO jos_jxgallery_assign_ratings_temp( image_id, pscore, hits, contest_id )
SELECT jos_jxgallery_images.image_id, jos_jxgallery_images.pscore, jos_jxgallery_images.hits, jos_jxgallery_contests.contest_id
FROM jos_jxgallery_images, jos_jxgallery_contests
INNER JOIN jos_jxgallery_contest_image_map ON jos_jxgallery_images.image_id = jos_jxgallery_contest_image_map.image_id
INNER JOIN jos_jxgallery_contests ON jos_jxgallery_contest_image_map.contest_id = jos_jxgallery_contests.contest_id
WHERE jos_jxgallery_contests.published =1
AND jos_jxgallery_images.published =1
ORDER BY jos_jxgallery_images.pscore DESC , jos_jxgallery_images.hits DESC
LIMIT 25
MySQL said: Documentation
#1066 - Not unique table/alias: 'jos_jxgallery_contests'
Now when I change the query to this...I get another error listed below the query. Any ideas?
INSERT INTO jos_jxgallery_assign_ratings_temp( image_id, pscore, hits, contest_id )
SELECT jos_jxgallery_images.image_id, jos_jxgallery_images.pscore, jos_jxgallery_images.hits, jos_jxgallery_contests.contest_id
FROM jos_jxgallery_images, jos_jxgallery_contests
INNER JOIN jos_jxgallery_contest_image_map ON jos_jxgallery_images.image_id = jos_jxgallery_contest_image_map.image_id
INNER JOIN jos_jxgallery_contests AS con ON jos_jxgallery_contest_image_map.contest_id = con.contest_id
WHERE con.published =1
AND jos_jxgallery_images.published =1
ORDER BY jos_jxgallery_images.pscore DESC , jos_jxgallery_images.hits DESC
LIMIT 25
MySQL said: Documentation
#1054 - Unknown column 'jos_jxgallery_images.image_id' in 'on clause'
You use the jos_jxgallery_contests in FROM and INNER JOIN. In INNER JOIN you make a ambiguous connection. Why you try to do that?
Related
I'm trying to update 1 field from another table's data but I'm getting the error #1242 - Subquery returns more than 1 row.
The query I'm running is:
UPDATE oc_order AS o
SET o.date_added = ( SELECT date_added FROM oc_order_history
WHERE order_id = o.order_id
AND order_status_id = 5)
Any ideas on where it's going wrong or how I can fix?
You should use JOIN.
UPDATE oc_order oc
INNER JOIN
(
SELECT date_added ,order_id
FROM oc_order_history
WHERE order_status_id = 5
) as oh on oc.order_id=oh.order_id
set oc.date_added=oh.date_added ;
I supposed that order_id is the key for JOIN condition.
Test it and let me know if it helps.
It would be better if you gave some examples data to get a right answer.
It's probably something I can't seem to understand with MySQL, but after wasting my day going through StackOverflow's related questions without fixing the issue, I decided to ask about it.
SELECT users.idUser, users.name, categoryName
FROM users
LEFT JOIN (
SELECT `translation` as categoryName
FROM localization,
usercategories
WHERE localization.`string` = usercategories.name
AND usercategories.idUserCategory = users.idUserCategory
)
as Something
WHERE users.idUser != 1
ORDER BY users.name ASC
Whichever query I tried today that would include a subquery, I would get the same syntax error at pretty much the same place: right after the subquery's alias (in this case, Something).
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE users.idUser != 1 ORDER BY users.name ASC LIMIT 0, 30' at line 11
This issue here is that you're missing the ON clause of your join. You need to select a condition to join the two tables together, like this:
SELECT stuff
FROM stuff
LEFT JOIN (other stuff)
ON stuff.something = otherstuff.something. // Add here.
You're JOIN criteria is non-ANSI and does not have an ON clause ... perhaps that is causing it? Try this, a bit more optimized:
SELECT Usr.idUser AS idUser
,Usr.name AS name
,UsrCat.translation AS categoryName
FROM users AS Usr
LEFT OUTER JOIN usercategories AS UsrCat
ON UsrCat.idUserCategory = Usr.idUserCategory
LEFT OUTER JOIN localization AS Lcl
ON Lcl.string = UsrCat.name
WHERE Usr.idUser <> 1
ORDER BY Usr.name ASC
No need for subquery, should be pretty performant.
You could re-organize your query so that it does not need a sub-query. This would also allow you the benefit of adding more columns to the select from any of the tables. Also, it is more correct.
SELECT
users.idUser,
users.name,
localization.`translation` as categoryName
FROM users
LEFT JOIN usercategories ON usercategories.idUserCategory = users.idUserCategory
LEFT JOIN localization ON localization.`string`= usercategories.name
WHERE users.idUser <> 1
ORDER BY users.name ASC
Initially I need to build a query fetching sites from one table ordered by date of newest article (articles placed in the separate table).
I build the following query:
SELECT *
FROM `sites`
INNER JOIN `articles` ON `articles`.`site_id` = `sites`.`id`
ORDER BY `articles`.`date` DESC
GROUP BY `sites`.`id`
I supposed that SELECT and INNER JOIN will fetch all posts and associate a site to each one, than ORDER BY will order the result by descending of post date than GROUP BY will take the very first post for each site and I will get the needed result.
But I'm receiving MySQL error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BYsites.idLIMIT 0, 30' at line 7
If I place GROUP BY before ORDER BY statement the query is working but it will not give me the newest post for each site. Instead the result will be sorted after the grouping which is not the thing I need (actually I could prefer to order in another way after grouping).
I read several pretty similar questions but they all related to the data stored in a single table making it possible to use MAX and MIN functions.
What should I do to implement what I need?
You can use either a subquery / derived-table / inline-view or a self-exclusion join, e.g.:
SELECT s.*, a1.*
FROM `sites` s
INNER JOIN `articles` a1 ON a1.`site_id` = s.`id`
LEFT OUTER JOIN `articles` a2 ON a2.`site_id` = a1.`site_id`
AND a2.`date` > a1.`date`
WHERE
a2.`site_id` IS NULL
ORDER BY
a1.`date` DESC
The principle is that you select the sites for which there is no article date greater than any other article date.
rewrite the sql to the following syntax -
SELECT `articles`.`article_name`,'sites'.'id','articles'.'site_id'
FROM `sites`,'articles'
WHERE `articles`.`site_id` = `sites`.`id`
ORDER BY 'sites'.'id', `articles`.`date` DESC;
Do something like this in the select statement. Group by function demands that all fields to be grouped. Hence usage of * is not possible.
SELECT * FROM ( SELECT `S.<col1>`, `S.<col2>`, `A.<col1>`,`A.<col2>`,
ROW_NUMBER ()
OVER (PARTITION BY `SITES`.`ID`
ORDER BY `SITES`.`ID` DESC)
RID
FROM `SITES` `S`,`ARTICLES` `A`
WHERE `ARTICLES`.`SITE_ID` = `SITES`.`ID`
)
WHERE RID = 1;
Can you try this?
Finally I came to the solution.
First of all I changed the main query from queering from sites table to queering from articles. Next I added the MAX(date) column to the result.
So the resulting query implementing the thing I need is the following:
SELECT `sites`.`url`,MAX(`articles`.`date`) AS `last_article_date`
FROM `articles`
INNER JOIN `sites` ON `sites`.`id` = `article`.`site_id`
GROUP BY `site_id`
ORDER BY `last_article_date` ASC
Thanks to all of you for giving me hints and right search directions!
This is my mysql query
SELECT tm.MAGAZINE_ID, tm.MAGAZINE_NAME,tm.MAGAZINE_DESCRIPTION,pub.publisher_name,
tmi.COVER_PAGE_THUMB AS COVER_PAGE_VERTICAL,tmi.FROM_DATE AS ISSUE_DATE,
tm.html_flag AS HTML_EXIST,tm.CATEGORY_ID,tm.language_id,tm.is_free,tma.AppUrl,
(SELECT issue_id from tbl_magazine_issue WHERE magazine_id = 141
ORDER BY FROM_DATE DESC LIMIT 1) as temp_issue_id
FROM tbl_magazine_apps as tma
LEFT OUTER JOIN tbl_magazine_code as tmc ON tmc.Code = tma.AppsCode
LEFT OUTER JOIN `tbl_magazine` AS tm ON tmc.magazine_Id = tm.MAGAZINE_ID
JOIN `tbl_magazine_issue` AS tmi ON temp_issue_id = tmi.issue_id
LEFT OUTER JOIN mst_publisher AS pub ON tm.publisher_id=pub.publisher_id
WHERE
tmi.PUBLISH_STATUS IN(1,3)
AND tmi.`OS_SELECT` = '".$osType."'
AND tma.id IN (".$appIds.")
GROUP BY tm.MAGAZINE_ID
ORDER BY tmi.ISSUE_DATE DESC
but i got an error that
#1054 - Unknown column 'temp_issue_id' in 'on clause'
if any one know about this please help me. i am new to this
AFAIK The subquery belongs to the from part:
http://dev.mysql.com/doc/refman/5.7/en/from-clause-subqueries.html
So I would join the subquery.
Like:
SELECT a.a, b.b
FROM table1 as a
JOIN (SELECT b from table2) as b ON a.key = b.key;
As the message suggests, the column temp_issue_id is not in any of the following tables: tbl_magazine_apps, tbl_magazine, or tbl_magazine_issue. It is also not a variable in the environment.
Beyond that, it is pretty much impossible for anyone to figure out how to fix the problem without more knowledge about the data structure.
If I were to hazard a guess, based on the table names, that particular join would be:
JOIN `tbl_magazine_issue` AS tmi ON tm.magazine_id = tmi.magazine_id
because it makes sense to me that a magazine issue would be connect to a magazine. I have no idea what temp_issue_id is, though.
temp_issue_id give alias name for specifying this column i think it should be tm.issue_id
temp_issue_id is column's name as per the query. You need it to convert to table and use subsequent column in your SELECT clause.
I have this query:
SELECT
course_category.id AS languageId,
course_category.code AS languageCode,
course_category.name AS languageName,
(
SELECT
SUM(gradebook_result.score)
FROM
gradebook_result
JOIN
gradebook_evaluation ON gradebook_evaluation.id=gradebook_result.evaluation_id
JOIN
gradebook_category ON gradebook_category.id=gradebook_evaluation.category_id
WHERE
gradebook_category.course_code=course_category.code
) AS languageWordsTranslated
FROM
course_category
WHERE
course_category.code != 'GEN'
ORDER BY
name
ASC
The problem occurs inside the nested SELECT where I get an error referencing the course_category table from within the SELECT on line:
WHERE
gradebook_category.course_code=course_category.code
Giving error:
Unknown column 'course_category.code' in 'where clause'
I have done this query before with other projects the only difference is that this one has joins within it. Any ideas?
EDIT: I removed the joins and hardcoded:
WHERE
course_category.code = 'ARA'
Seems like the joins mess it up, any way to fix that??
Try to do it a bit different way, using subquery and joining to it:
SELECT
course_category.id AS languageId,
course_category.code AS languageCode,
course_category.name AS languageName,
t.total AS languageWordsTranslated
FROM
course_category
JOIN (
SELECT gradebook_category.course_code, SUM(gradebook_result.score) as total
FROM gradebook_result
JOIN gradebook_evaluation ON gradebook_evaluation.id=gradebook_result.evaluation_id
JOIN gradebook_category ON gradebook_category.id=gradebook_evaluation.category_id
GROUP BY gradebook_category.course_code
) t ON t.course_code = course_category.code
WHERE
course_category.code != 'GEN'
ORDER BY
name
ASC