MySQL error code 1064 syntax error - mysql

This is a very specific question to the kind of code i have written for postgresql and i am migrating to mysql for project requirements.
The code written so far in mysql is as follows :
(select substring(dt,1,9) as dt,concat(vish,visl,visn) as vis,ip
from assignment_walmart.b
where service='ss' and ua not like '%ktxn%'
and ua not like '%khte%'
and ua not like '%keynote%'
group by 1,2,3
) as A1
left join // This is where it shows the error.
(select ip,flag from
assignment_walmart.b1
group by 1,2
) as A2
on A1.ip=A2.ip
where A2.flag is NULL
group by 1,2;
The error is popping up near the naming of the two selected tables as "A1" and "A2", so i'm assuming it's not allowed in mysql.
Can you please help me with an alternate syntax for the above code as I have to use the two tables in this manner only to join in to get required results.
How exactly do i use alias or join 2 tables in such a manner which was clearly working in postgresql?
Any help would be appreciated.

You have a subquery joined to another query. This shouldn't work in either database. You need to wrap these in a select or something like that:
select A2.dt, A2.vis, count(*)
from (select substring(dt,1,9) as dt, concat(vish,visl,visn) as vis,ip
from assignment_walmart.b
where service='ss' and ua not like '%ktxn%'
and ua not like '%khte%'
and ua not like '%keynote%'
group by substring(dt,1,9), concat(vish,visl,visn), ip
) as A1 left join // This is where it shows the error.
(select ip,flag from
assignment_walmart.b1
group by ip, flag
) as A2
on A1.ip=A2.ip
where A2.flag is NULL
group by A2.dt, A2.vis;
I am making a guess on what you want for the outer query and what the aggregation fields are. It is a good idea to be explicit about which fields are being aggregated.

It looks like you are missing the SELECT ... FROM on the outer query.
It looks you mean for your query to be of the form:
SELECT ...
FROM ( inline view query ) A1
LEFT
JOIN ( inline view query ) A2
ON A1.col = A2.col ...
WHERE ...
GROUP BY ...

Related

MySQL DISTINCT returning not so distinct results

Good day,
I have a small issue with MySQL Distinct.
Trying the following query in my system :
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`, `bookingcomment_message` FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791
The point is that there are bookings like 29791 that have many comments added.
Let's say 10. Then when running the above query I see 10 results instead of one.
And that's not the way DISTINCT supposes to work.
I simply want to know if there are any comments. If the comment ID is not 0 then there is a comment. Of course I can add COUNT(blabla) as comment_number but that's a whole different story. For me now I'd like just to have this syntax right.
You may try aggregating here, to find which bookings have at least a single comment associated with them:
SELECT
b.booking_id,
b.booking_ticket,
b.booking_price
FROM mysystem_booking b
LEFT JOIN mysystem_bookingcomment bc
ON b.booking_id = bc.bookingcomment_link
WHERE
b.booking_id = 29791
GROUP BY
b.booking_id
HAVING
COUNT(bc.bookingcomment_link) > 0;
Note that depending on your MySQL server mode, you might have to also add the booking_ticket and booking_price columns to the GROUP BY clause to get the above query to run.
You can try below - using a case when expression
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`,
case when `bookingcomment_message`<>'0' then 'No' else 'Yes' end as comments
FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791

mysql JOIN Inside a JOIN and search a group_concatenated values

I cant seem to find a solution for Searching a group_concatenated value,
I have 3 table that are connected with id's
1st table have the same value with 2nd table, but no same value with 3rd,
2nd table have the same value with 1st and 3rd table,
I want to get the value inside 3rd table,
concat the values in accordance to Distinct ID's of 2nd table, display them, and be able to search
this are my tables look like
how do i search for the concatenated values
please if there's a better way, your help is much appreciated?
the query below is what i have so far
$query = $db->prepare("
SELECT
a.problem_encountered,
GROUP_CONCAT(
DISTINCT
c.full_name)
AS
fnames
FROM
maintenance_sheet_table a
LEFT JOIN
mis_incharge_table b
ON
b.mis_incharge_id = a.mis_incharge_id
INNER JOIN
users_table c
ON
c.mis_id=b.mis_id
WHERE
a.problem_encountered
LIKE
:findMe
HAVING
fnames
LIKE
:findMe
GROUP BY a.id ORDER BY a.id
");
$query->bindValue(':findMe', '%' . $keywordSearch. '%');
A potential answer is to filter the Users_table in a subquery. There are a number of different forms of this option, and hard to tell from your data which is required. The one I have below simply returns the users that match the search criteria.
SELECT a.problem_encountered, GROUP_CONCAT(DISTINCT innerc.full_name) AS fnames
FROM maintenance_sheet_table a
LEFT JOIN mis_incharge_table b ON b.mis_incharge_id = a.mis_incharge_id
LEFT JOIN (SELECT c.mis_id, c.full_name
FROM users_table c
WHERE c.full_name LIKE :findMe) innerc ON innerc.mis_id=b.mis_id
WHERE a.problem_encountered LIKE :findMe
GROUP BY a.id
ORDER BY a.id
However, you could also do the concatenation within the subquery if required.
SELECT a.problem_encountered, innerc.fnames
FROM maintenance_sheet_table a
INNER JOIN (SELECT mit.mis_incharge_id, GROUP_CONCAT(DISTINCT ut.full_name) AS fnames
FROM users_table ut
INNER JOIN mis_incharge_table mit ON ut.user_id = mit.user_id
GROUP BY mit.mis_incharge_id
HAVING fnames LIKE :findMe) innerc ON innerc.mis_incharge_id = a.mis_incharge_id
WHERE a.problem_encountered LIKE :findMe
GROUP BY a.id
ORDER BY a.id
Note: I agree with spencer7593, that you shouldn't use the same :findMe variable against 2 separate fields. Even if it works, to a maintenance programmer or even yourself in a few years time, will probably look at this and think that the wrong fields are being interrogated.
You can "search" the return from the GROUP_CONCAT() expression in the HAVING clause. As a more efficient alternative, I suspect you could use an EXISTS predicate with a subquery.
I suspect part of the problem is that your query is referencing the same bind placeholder more than one time. (In previous releases of PDO, this was a restriction, a named bind placeholder could be referenced only once.)
The workaround to this issue is to use a separate bind placeholder, e.g.
HAVING fnames LIKE :findMeToo
And then bind a value to each placeholder:
$query->bindValue(':findMe', '%' . $keywordSearch. '%');
$query->bindValue(':findMeToo', '%' . $keywordSearch. '%');
(With this issue, I don't think PDO issued a warning or error; the effect was as if no value was supplied for the second reference to the named bind placeholder. Not sure if this issue is fixed, either by a code change or a documentation update. The workaround as above, reference a bind placeholder only once within a query.)
Beyond that, it's not clear what problem you are observing.
Your HAVING clause should come after your GROUP BY clause
change
HAVING
fnames
LIKE
:findMe
GROUP BY a.id ORDER BY a.id
to
GROUP BY a.id
HAVING
fnames
LIKE
:findMe
ORDER BY a.id

How do I convert this SQL query into hibernate

Can someone please tell me how to convert this SQL query into hibernate?
SELECT * FROM sys_quote_master AS g1
INNER JOIN
(SELECT order_base_id, order_id FROM sys_quote_master
GROUP BY order_base_id, order_date_last_revised
ORDER BY order_date_last_revised desc) AS g2
ON g2.order_id = g1.order_id;
Basically,
I have tried this and it doesn't work:
DetachedCriteria crit1 = DetachedCriteria.forClass(QuoteMaster.class);
ProjectionList pList = Projections.projectionList();
pList.add(Projections.groupProperty("orderBaseId"));
session = HibernateSessionFactory.currentSession();
Criteria crit = session.createCriteria(QuoteMaster.class);
Criteria c = crit.createCriteria("QuoteMaster", CriteriaSpecification.INNER_JOIN);
c.setProjection(pList);
I get this error:
org.hibernate.QueryException: could not resolve property: this of:
QuoteMaster
And I think it has to do with this line of code were I am trying to create an inner join to the same table:
Criteria c = crit.createCriteria("QuoteMaster", CriteriaSpecification.INNER_JOIN);
So basically, my question is 'How to create a join to the same table using Criteria.createCriteria or Criteria.createAlias?' The doc for Criteria class states the first parameter is a dot-separated property path, but what the heck does that mean? :)
Every example I found so far shows how to do this with 2 or 3 tables but not to the same table and I have no idea what to use for the first argument.
I dont know how your entities are, this should give rough idea.
from SystQuoteMasterEntity sqm join ( select sqm1.orderbaseid, sqm1.orderId from SystQuoteMasterEntity sqm1 group by orderbaseid,orderdatelastrev order by orderdatelast desc) g2

Correlated Subquery in a MySQL CASE Statement

Here is a brief explanation of what I'm trying to accomplish; my query follows below.
There are 4 tables and 1 view which are relevant for this particular query (sorry the names look messy, but they follow a strict convention that would make sense if you saw the full list):
Performances may have many Performers, and those associations are stored in PPerformer. Fans can have favorites, which are stored in Favorite_Performer. The _UpcomingPerformances view contains all the information needed to display a user-friendly list of upcoming performances.
My goal is to select all the data from _UpcomingPerformances, then include one additional column that specifies whether the given Performance has a Performer which the Fan added as their favorite. This involves selecting the list of Performers associated with the Performance, and also the list of Performers who are in Favorite_Performer for that Fan, and intersecting the two arrays to determine if anything is in common.
When I execute the below query, I get the error #1054 - Unknown column 'up.pID' in 'where clause'. I suspect it's somehow related to a misuse of Correlated Subqueries but as far as I can tell what I'm doing should work. It works when I replace up.pID (in the WHERE clause of t2) with a hard-coded number, and yes, pID is an existing column of _UpcomingPerformances.
Thanks for any help you can provide.
SELECT
up.*,
CASE
WHEN EXISTS (
SELECT * FROM (
SELECT RID FROM Favorite_Performer
WHERE FanID = 107
) t1
INNER JOIN
(
SELECT r.ID as RID
FROM PPerformer pr
JOIN Performer r ON r.ID = pr.Performer_ID
WHERE pr.Performance_ID = up.pID
) t2
ON t1.RID = t2.RID
)
THEN "yes"
ELSE "no"
END as pText
FROM
_UpcomingPerformances up
The problem is scope related. The nested Selects make the up table invisible inside the internal select. Try this:
SELECT
up.*,
CASE
WHEN EXISTS (
SELECT *
FROM Favorite_Performer fp
JOIN Performer r ON fp.RID = r.ID
JOIN PPerformer pr ON r.ID = pr.Performer_ID
WHERE fp.FanID = 107
AND pr.Performance_ID = up.pID
)
THEN 'yes'
ELSE 'no'
END as pText
FROM
_UpcomingPerformances up

Select column or null?

Can I select like this?
SELECT DISTINCT idClient, idAcc,Description
FROM client, account
WHERE (account.idCliente = client.idCliente
OR account.idCliente is NULL )
Im getting trouble because it show to me duplicated results :x How can I do it ?
Thanks
EDIT:
RESULTS
idClient idAcc Description
1 3 good
1 2 bad
1 3 bad
Note that im getting 2 diferent Descriptions for same idAcc
EDIT2:
I realy need that search by NULL or Not NULL.
You are using an implicit join. Try using an explicit JOIN like so:
SELECT DISTINCT Description
FROM client
LEFT JOIN account ON account.idCliente = client.idCliente
I would rewrite your query to use a LEFT JOIN so that you get all situations where there is a client.idCliente, but also those where the account.idCliente doesn't exist.
Like so:
SELECT DISTINCT Description
FROM client
LEFT JOIN account ON client.idCliente = account.idCliente;