Syntax error in MySQL Join Query - mysql

I'm getting a syntax error in MySQL query. Is MySQL and SQL server work differently? Can anyone suggest, what is wrong and where ?
select b.component, d.matter, d.bug, d.timestamp, d.os
from bugs.profiles p, ops_reports.BPR_TAG_DATA d
left join (Select * from bugs where product='test') b
on d.bug=b.bug_id
where d.tagid = 6
and timestamp between "2014-04-21" and "2014-04-24"
and login_name like 'test'
and p.userid = d.user
Error Message 24/04/2014 23:14:10 0:00:00.037 MySQL Database Error: 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 'Select * from bugs where product='Conversions') as b
on (d.bu 1 0

You should not mix implicit and explicit joins. A simple rule: just don't use commas in the from clause.
select b.component, d.matter, d.bug, d.timestamp, d.os
from ops_reports.BPR_TAG_DATA d left join
bugs b
on b.product = 'test' and d.bug = b.bug_id left join
bugs.profiles p
on p.userid = d.user
where d.tagid = 6 and
timestamp between '2014-04-21' and '2014-04-24' and
login_name like 'test';
I also removed the subquery, moving the condition to the on clause. This makes the query more efficient. And changed the delimiters for the date constants to single quotes. Using double quotes for strings can lead to confusion.
EDIT:
All this said, the query in the question looks like it is syntactically correct. I notice that the error message does not refer to this exact query. The query has product='test') b and the error message has product='Conversions') as b. Perhaps there are other differences as well.

Related

How to resolve wrong syntax near HAVING COUNT DISTINCT?

This it the code I am trying to execute:
SELECT ID_K
FROM koncert,
programi
WHERE koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT (DISTINCT programi.Salla) = 2
It returns this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use
near 'DISTINCT programi.Salla)=2 LIMIT 0, 25' at line 4.
Tried to change different things but it still won't work .
You should use the count(DISTINCT programi.Salla
) and not count (..) ..remove space between COUNT and (...
SELECT koncert.ID_K
FROM koncert
INNER JOIN programi on koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT(DISTINCT programi.Salla) = 2
but you need also tablename for avoid ambiguity and use explicit join sintax too
First you should use qualified name for your column, when column name is same in both table
SELECT ID_K FROM
should be
SELECT programi.ID_K FROM
else, you will get ambiguous column error. Otherwise, your query looks fine except removing extra space when calling COUNT (#spencer already mentioned in comment)
Also, it is good practice to join your table using JOIN (or INNER JOIN, LEFT JOIN etc) keyword, which makes your query more clear and readable.

MySQL join error

The following query gives me an error in phpmyadmin. It looks syntactically correct to me, and the table/column names match up accordingly. I have tried a number of variations (quoting table names, using as, etc) with no luck.
SELECT *
FROM GROUP
INNER JOIN GROUP_MEMBER ON GROUP.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';
Error I'm getting:
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 INNER JOIN GROUP_MEMBER ON GROUP.group_id = GROUP_MEMBER.group_id WHERE ' at line 2
"group" is a sql-keyword, you need to surround it with backticks if you want to use it as tablename.
GROUP is a reserved word in SQL so it's a bad choice for a table name. If you surround it with backticks it might work but I'd really recommend changing that table name.
SELECT *
FROM `GROUP`
INNER JOIN GROUP_MEMBER ON `GROUP`.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';
This is not PHPMyAdmin-specfiic error. The problem you have is using a table name GROUP that matches a MySQL reserved word. If you insist on using such a problematic table name, you need to enclose it with backticks anywhere you might use it.
SELECT *
FROM `GROUP`
INNER JOIN GROUP_MEMBER ON `GROUP`.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';

Joining derived tables

EDIT: I am using phpMyAdmin interface, and I have been copy/paste the codes from phpMyAdmin to here. The phpMyAdmin seems to run a "different code" as I run the following code, and generating some error message that are referring to that "different code", causing huge confusion.
** Final edit: It seems Safari is causing this: it run the "different query" when I try to run 2nd query below. Use Firefox instead, and it generate correct results. Thanks for the help and sorry for the confusion. **
I have two tables: newsFeeds, comments, where
** newsFeeds contains column PID, comments contains column FID.**
I want to join rows in two tables with matching PID = FID. My code is:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
)
ON comments.FID = newsFeeds.PID
and the error message is "#1248 - Every derived table must have its own alias".
I then add in AS newtb after ) according to other posts here. And the code is then:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
) AS newtb
ON newtb.FID = newsFeeds.PID
But another error shows up: #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 'INNER JOIN( SELECTFID,COUNT(*) AScount FROMcomments
LIMIT 0, 25' at line 8
I wonder how to correctly do this?
You should correct this by removing the derived table:
SELECT *
FROM tb_1 INNER JOIN
tb_2
ON tb_2.FID = tb_1.PID;
MySQL has a tendency to materialize derived tables, which hurts performance.
The answer to your question, though, is to add a name after the parentheses:
SELECT *
FROM tb_1 INNER JOIN
(SELECT *
FROM tb_2
) t2
ON t2.FID = tb_1.PID;

Sql query doesn't work in Mysql version 5.5.16

I am attempting to execute the following query:
$query = "SELECT O. * AS NUM FROM (ab_order O)
LEFT JOIN ab_user U ON (O.id_user=U.id)
WHERE O.id IN ( SELECT OT.id_ab_order FROM ab_order_transaction OT
LEFT JOIN ab_transaction T ON (OT.id_ab_transaction = T.id)
LEFT JOIN ab_user U ON (T.id_user = U.id)
WHERE T.validated = 1 {$condTrans} ) {$condOrder}
ORDER BY {$orderCol} LIMIT $from, $numRecords ";
$queryDB = $DB->queryExec($query);
On the live server:
it works with MySql version 3.3.10.
PHP version 5.2.17.
But I need to use localhost:
XAMPP for linux, v. 1.7.7
PHP 5.3.8
MySql version 5.5.16.
On localhost it says:
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 'AS NUM
FROM (ab_order O)
LEFT JOIN ab_u' at line 1.
Is any easier way then uprgrade the live server Mysql database?
The aliasing of all the columns seems incorrect: SELECT O. * AS NUM. I'm unsure of why it would work on previous versions, but the as num should either be removed, or each column should be explicitly aliased.
You can define Alias for individual column in SELECT. Here is the issue
SELECT O. * AS NUM
Instead of this use
SELECT O. *
This is useful article Using Column Alias in SELECT Statement

mySql Error in Query

I have (3) tables: Professor, Comment, Course
I'm trying to get a few columns to display the professor with all the courses and all the comments they have.
My tables
SQL:
SELECT prefix, code, info, date
FROM Course, Comment
JOIN Professor ON Professor.pID = Comment.pID
AND JOIN Course ON Course.cID = Comment.cID
WHERE Comment.pID = ?
Throws error:
Syntax error or access violation: 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 'JOIN Course ON Course.cID = Comment.cID WHERE Comment.pID = '273'' at line 4
SELECT prefix, code, info, date
FROM Comment
JOIN Professor ON Professor.pID = Comment.pID
JOIN Course ON Course.cID = Comment.cID
WHERE Comment.pID = ?
Just remove the AND before the second JOIN and remove Course in the From list. You may need to change the order of the tables depending on what table data you are primarily after.
Advise that you have a read through the MySQL JOIN syntax page.
change 'AND JOIN' to just 'JOIN'
There is no AND JOIN. Please read about JOIN syntax in the manual before attempting to use it.
Additionally, you reference table Course twice.