I wanted to combine 11 columns of a form into single column.
here is my query
SELECT DormData.BuildingID,
BuildingDetails.BuildingName,
Contractors.Item,
ActionDetails.ActionType,
(DormData.Note1
+ DormData.Note2
+ DormData.Note3
+ DormData.Note4
+ DormData.Note5
+ DormData.Note6
+ DormData.Note7
+ DormData.Note8
+ DormData.Note9
+ DormData.Note10
+ DormData.Note11) AS Notes
FROM ActionDetails
INNER JOIN (Contractors
INNER JOIN (BuildingDetails
INNER JOIN DormData
ON BuildingDetails.BuildingID = DormData.BuildingID)
ON Contractors.ID = DormData.ItemID)
ON ActionDetails.ActionID = DormData.ActionID;
But not getting required result.
You need to replace the + with a &
Related
native Query I Wrote in My Repository
If i remove order by condition in the below query , it is giving some records but i want it in some order so i added order by condition then it not showing records only . but the same mysql query with order by condition is giving some records in mysql workbench.
#Query(value = "SELECT l.id AS id,l.first_name AS firstName,l.last_name AS lastName,"
+ " l.email AS email,l.phone AS phone,l.place AS place,l.course_id AS courseId,"
+ " c.name AS courseName,l.source AS source,l.enquiry_for AS enquiryFor,"
+ " l.appointment_date AS appointmentDate,l.description AS description,"
+ " l.discount AS discount,l.status AS status,l.assignee_id AS assigneeId,"
+ " ase.first_name AS assigneeFirstName,ase.last_name AS assigneeLastName,"
+ " l.assignor_id AS assignorId,asr.first_name AS assignorFirstName,"
+ " asr.last_name AS assignorLastName,l.active AS active,l.created_date AS createdDate,"
+ " l.updated_date AS updatedDate,(SELECT comments FROM reviews where created_date IN"
+ " (SELECT MAX(created_date) from reviews where lead_id=l.id)) AS latestComment FROM "
+ " leads l JOIN users AS ase ON l.assignee_id = ase.id JOIN users AS asr ON "
+ " l.assignor_id = asr.id JOIN courses AS c ON l.course_id =c.id WHERE l.status!='Draft'"
+ " AND (l.first_name LIKE '%:keyword%' OR l.last_name LIKE '%:keyword%' OR l.phone LIKE"
+ " '%:keyword%') ORDER BY -l.appointment_date DESC,l.created_date ASC", nativeQuery=true)
List<Leads> searchLeadsForAdmin(#Param("keyword") String searchKeyword);
i don't know Where am i going wrong , attached image below mysql query result came in workbench.
I found the answer to my question. It was a concatenation issue. In the second last line of my query after 'l.phone LIKE' I added the value in the next line.
+ " AND l.first_name LIKE %:keyword% OR l.last_name LIKE %:keyword% OR "
+ " l.phone LIKE %:keyword% ORDER BY -l.appointment_date DESC,"
+ " l.created_date ASC", nativeQuery = true)
The above query works.
I have this query:
SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table
Is there a way to add these values count_1 + count_2 + count_3 + count_4 + count_5 + count_6 and so on.. more efficiently? MySQL keeps crashing for me when I add huge numbers of fields.
Regardless of whether the db design is right or wrong if you use an aggregation function you should use group by
SELECT name, SUM(count_1 + count_2 + count_3 + count_4 + count_5 + count_6) AS Total
FROM my_table
GROUP BY name
My Query:
entityManager.createQuery("SELECT " +
"q.id, " +
"q.title, " +
"q.user.fullName, " +
"q.user.reputationCount, " +
"q.viewCount, " +
"q.countValuable, " +
"q.persistDateTime, " +
"t.id, " +
"t.name, " +
"t.description, " +
"(SELECT COUNT (a) FROM Answer a WHERE a.question.id = q.id), " +
"(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id) " +
"FROM Question q JOIN q.tags t")
Here I get the error - [21000][1242] Subquery returns more than 1 row
By the method of exceptions, I determined that the error in this query string:
"(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id) "
How to make the correct request so that there is no this error? Thank!
Two common ways are aggregation and limiting:
(SELECT MAX(a.isHelpful) FROM Answer a WHERE a.question.id = q.id)
(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id LIMIT 1)
However, those are really just hacks to get around an "issue" with the data. I put issue in quotes, but the real issue is probably your understanding of data and not the data itself.
You should understand why there are duplicates. Then decide which value you want. And implement the correct logic for what you want.
Subquery returns more than 1 row, this simply means that your query is not returning a single row for the outer select statement to work.
"(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id) "
you have to apply a set of conditions to filter out your data uniquely or use joins to combine your table Answer and Question and then filter data accordingly.
you can also group each row data in one column by GROUP_CONCAT Mysql function like this :
"(SELECT GROUP_CONCAT(a.isHelpful) FROM Answer a WHERE a.question.id = q.id) "
Although GROUP_CONCAT is not available in Mysql, for that you can also bind SQL function in hibernate as described in this post.
After a day of various trial and error, I found the following solution, I hope someone will broaden their horizons and help in solving their problem:
entityManager.createQuery("SELECT " +
"q.id, " +
"q.title, " +
"q.user.fullName, " +
"q.user.reputationCount, " +
"q.viewCount, " +
"q.countValuable, " +
"q.persistDateTime, " +
"t.id, " +
"t.name, " +
"t.description, " +
"(SELECT COUNT (a) FROM Answer a WHERE a.question.id = q.id), " +
"(SELECT CASE WHEN MAX (a.isHelpful) > 0 THEN true ELSE false END FROM Answer a WHERE a.question.id = q.id) " +
"FROM Question q JOIN q.tags t")
I'm trying to make a calculated value to show in another column in another table.
Can someone please explain why this doesn't work
CREATE TABLE #Medition (ID int,AVG decimal(18,4))
INSERT INTO #Medition (ID, AVG)
SELECT ID, SUM(125Hz + 250Hz + 500Hz + 750Hz + 1000Hz + 1500Hz + 2000Hz + 3000Hz + 4000Hz + 6000Hz + 8000Hz)/11 AS AVG FROM tonvarden
UPDATE matningar SET matningar.tonmedelvarde =
#Medition.AVG FROM matningar INNER JOIN #Medition ON matningar.ID =#Medition.ID
DROP TABLE #Medition
I am getting this 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 'INSERT INTO #Medition (ID, AVG) SELECT ID, SUM(125Hz + 250Hz + 500Hz + 750Hz + ' at line 2
No need to create a temporary table to do this.
UPDATE matningar a
join tonvarden b on a.ID = b.ID
set a.tonmedelvarde = (`125Hz` + `250Hz` + `500Hz` + `750Hz` + `1000Hz` +
`1500Hz` + `2000Hz` + `3000Hz` + `4000Hz` + `6000Hz` +
`8000Hz`)/11;
If you would like to update matningar whenever a new row is inserted into tonvarden, then you can create the following trigger:
create trigger update_matningar before insert on tonvarden
for each row
update matningar
set tonmedelvarde =
(new.`125Hz` + new.`250Hz` + new.`500Hz` + new.`750Hz`
+ new.`1000Hz` + new.`1500Hz` + new.`2000Hz`
+ new.`3000Hz` + new.`4000Hz` + new.`6000Hz`
+ new.`8000Hz`)/11
where id = new.id;
Good Evening,
I have this query and i can't manage to fix it. It never finishes but it finishes on MSSQL 2000, i want to have it on MySQL. Any advice ?
SELECT T_Coll.Nom_REGION AS Region_Col,
T_Coll.Nom_STE AS Societe_Col,
T_Coll.Nom_ETS AS Ets_Col,
T_Coll.Nom_CDP AS CDP_Col,
T_Coll.ID_Coll,
T_Coll.NomColl AS NomColl,
T_Coll.Nom_Coll AS NOM,
T_Coll.Prenom_Coll AS Prenom,
T_Coll.Trig_Coll,
T_Coll.EXTERN,
T_DETAIL.AN,
T_DETAIL.Mois_Modif,
T_DETAIL.T_AFFAIRE_ID,
T_DETAIL.T_LOT_ID,
T_DETAIL.T_SOUS_LOT_ID,
T_Type.NomType,
T_DETAIL.AUTRE_LIBELLE,
T_DETAIL.JOUR01 + T_DETAIL.JOUR02 + T_DETAIL.JOUR03 + T_DETAIL.JOUR04 + T_DETAIL.JOUR05 + T_DETAIL.JOUR06 + T_DETAIL.JOUR07 + T_DETAIL.JOUR08 + T_DETAIL.JOUR09 + T_DETAIL.JOUR10 + T_DETAIL.JOUR11 + T_DETAIL.JOUR12 + T_DETAIL.JOUR13 + T_DETAIL.JOUR14 + T_DETAIL.JOUR15 + T_DETAIL.JOUR16 + T_DETAIL.JOUR17 + T_DETAIL.JOUR18 + T_DETAIL.JOUR19 + T_DETAIL.JOUR20 + T_DETAIL.JOUR21 + T_DETAIL.JOUR22 + T_DETAIL.JOUR23 + T_DETAIL.JOUR24 + T_DETAIL.JOUR25 + T_DETAIL.JOUR26 + T_DETAIL.JOUR27 + T_DETAIL.JOUR28 + T_DETAIL.JOUR29 + T_DETAIL.JOUR30 + T_DETAIL.JOUR31 AS Total_Jours,
T_DETAIL.JOUR01 + T_DETAIL.JOUR02 + T_DETAIL.JOUR03 + T_DETAIL.JOUR04 + T_DETAIL.JOUR05 + T_DETAIL.JOUR06 + T_DETAIL.JOUR07 + T_DETAIL.JOUR08 + T_DETAIL.JOUR09 + T_DETAIL.JOUR10 + T_DETAIL.JOUR11 + T_DETAIL.JOUR12 + T_DETAIL.JOUR13 + T_DETAIL.JOUR14 + T_DETAIL.JOUR15 + T_DETAIL.JOUR16 + T_DETAIL.JOUR17 + T_DETAIL.JOUR18 + T_DETAIL.JOUR19 + T_DETAIL.JOUR20 + T_DETAIL.JOUR21 + T_DETAIL.JOUR22 + T_DETAIL.JOUR23 + T_DETAIL.JOUR24 + T_DETAIL.JOUR25 + T_DETAIL.JOUR26 + T_DETAIL.JOUR27 + T_DETAIL.JOUR28 + T_DETAIL.JOUR29 + T_DETAIL.JOUR30 + T_DETAIL.JOUR31 + T_DETAIL.REGUL AS Total_Jours_et_Reg,
T_DETAIL.REGUL,
T_Cam.ID AS ID_Cram,
T_Cam.STATUT,
T_Cam.T_COLLABORATEUR_ID AS ID_Cram_Coll,
T_DETAIL.ID AS ID_Cram_Detail,
T_DETAIL.MOIS,
T_DETAIL.JOUR01,
T_DETAIL.JOUR02,
T_DETAIL.JOUR03,
T_DETAIL.JOUR04,
T_DETAIL.JOUR05,
T_DETAIL.JOUR06,
T_DETAIL.JOUR07,
T_DETAIL.JOUR08,
T_DETAIL.JOUR09,
T_DETAIL.JOUR10,
T_DETAIL.JOUR11,
T_DETAIL.JOUR12,
T_DETAIL.JOUR13,
T_DETAIL.JOUR14,
T_DETAIL.JOUR15,
T_DETAIL.JOUR16,
T_DETAIL.JOUR17,
T_DETAIL.JOUR18,
T_DETAIL.JOUR19,
T_DETAIL.JOUR20,
T_DETAIL.JOUR21,
T_DETAIL.JOUR22,
T_DETAIL.JOUR23,
T_DETAIL.JOUR24,
T_DETAIL.JOUR25,
T_DETAIL.JOUR26,
T_DETAIL.JOUR27,
T_DETAIL.JOUR28,
T_DETAIL.JOUR29,
T_DETAIL.JOUR30,
T_DETAIL.JOUR31,
T_Supr.Suppression,
T_Coll.TYPE
FROM T_Coll
INNER JOIN T_Cam ON T_Cam.T_COLLABORATEUR_ID = T_Coll.ID_Coll
INNER JOIN T_DETAIL ON T_DETAIL.T_Cam_ID = T_Cam.ID
INNER JOIN T_Type ON T_Type.TYPE = T_DETAIL.TYPE
LEFT OUTER JOIN T_Supr ON T_Supr.ID_Cram_Detail = T_DETAIL.ID
WHERE (T_Supr.Suppression IS NULL)
ORDER BY T_Coll.NomColl,
T_DETAIL.AN,
T_DETAIL.Mois_Modif,
T_DETAIL.T_AFFAIRE_ID,
T_DETAIL.T_LOT_ID,
T_DETAIL.T_SOUS_LOT_ID
I just can't manage to make this work under MySQL !
Thanks in advance !
All of the columns used in the joins, where clause and the order by should probably be indexed. You cannot expect any kind of decent performance in any database without indexes. There is nothing very complex about your query, so this is the most likely problem.
Do not return any columns you don't need. For instance why are you returning T_Supr.Suppression when the value will always be nulll based on your where clause?
In mysql you should look at the Explain Plan to see where the problem is with a slow query. In SQL Server look at the Execution PLan. You need to learn how to read these to effectively program in SQL.