Lets say that I have data rows that look like this:
+----------------------------+
+ test_table +
+----+--------+--------------+
+ id + word + updated_word +
+----+--------+--------------+
+ 1 + g00gle + ggle +
+ 2 + bread0 + bread +
+ 3 + 0bject + bject +
+ 4 + d0d0 + dd +
+-------------+--------------+
What statement could I use to take out the zeroes in the word column, so that it looks like the updated_word column? I thought of using substring, but didn't know how to proceed after that.
try:
UPDATE test_table
SET word = REPLACE(word, '0', '');
replace the 2nd blank '' with anything you want to change with.
On of the previous developers had a process that sent email alerts in the form of an html table in an email, this was done by triggering an stored procedure and passing the relevant parameters. This is a little outside my skills at the moment but im trying to get the same procedure to send an email for lateness. It works fine bar the formatting and layout. the query is -
SET #xml = CAST(( SELECT td = '<td align = "Center">' + CONVERT(VARCHAR(12),EmployeeName) + '</td>'
, td = '<td align = "Center">' + CONVERT(VARCHAR(12),StartTime) + '</td>'
, td = '<td align = "Center">' + CONVERT(VARCHAR(12),Late) + '</td>'
FROM #tempLateEmail
FOR XML PATH('tr'),TYPE).value('.','NVARCHAR(MAX)'
) AS NVARCHAR(MAX))
The body output is :
SET #body =
N'<style type="text/css">'
+ 'body {font-family: Arial;font-size:12px;} '
+ 'table{font-size:11px; border-collapse:collapse;table-layout: fixed} '
+ 'td{ border:1px solid black; padding:3px;} '
+ 'th{background-color:#F1F1F1;border:1px solid black; padding:3px;}'
+ 'h1{font-weight:bold; font-size:12pt}'
+ 'h2{font-weight:bold; font-size:10pt}'
+ '</style>'
+ '<body style="width:620px; margin:15px;">'
+ '<div style="margin:0 auto; width:300px">'
+ '<h1>Lateness Email on' + ' ' + convert(varchar, getdate(), 103) + '</h1>'
+ '</div>'
+ '<div style="float:left; width:250px">'
+ '<p>The following people appear to have been late today</p> '
+ '</div>'
+ '<div style="float:left; margin-left:20px; width:250px">'
+ '<div style="height:150px; overflow:auto">'
+ '<table width="250px">'
+ '<tr>'
+ '<th width = 100>Employee</th>'
+ '<th width =100>Expected Start Date</th>'
+ '<th width =100>Minutes Late</th>'
+ '</tr>'
+ #xml
+ '</table>'
+ '</div>'
+ '</div>'
+ '<div style="clear:both;"></div>'
+ '</body>'
SET #subject = 'Lateness Email for - ' + convert(varchar, getdate(), 103)
It then outputs them side by side instead of in a table. I understand why its doing it but I can not work out what to do about it. Hope i've provided enough info
as per Rohit Tiwari's comment there was indeed a <tr> missing from the end of the top secion of code -
SET #xml = CAST(( SELECT td = '<td align = "Center">' + CONVERT(VARCHAR(12),EmployeeName) + '</td>'
, td = '<td align = "Center">' + CONVERT(VARCHAR(12),StartTime) + '</td>'
, td = '<td align = "Center">' + CONVERT(VARCHAR(12),Late) + '</td>**<tr>** '
FROM #tempLateEmail
FOR XML PATH('tr'),TYPE).value('.','NVARCHAR(MAX)'
) AS NVARCHAR(MAX))
highlighted the update above.
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.
Below is what I have
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1 + 1 + +
+ 1 + + 1 +
+ 2 + 1 + +
+ 2 + + 2 +
++++++++++++++++++++++++
What I want is
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1 + 1 + 1 +
+ 2 + 1 + 2 +
++++++++++++++++++++++++
I want to combine the rows and show data for user in one row against multiple rows like I have in table.
Any idea how to do it?
Note : I don't have any row who have data for all fields. And I don't have any user with below scenario.
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 3 + 1 + +
+ 3 + 1 + 1 +
++++++++++++++++++++++++
Only 1 data in one row and two row per user.
I tried with
SELECT id, concat(field1), concat(field2) from myTable
GROUP BY id;
but its not coming.
data at sqlfiddle
You need to use one of MySQL's aggregate functions when aggregating grouped data. Either use GROUP_CONCAT() in place of (the non-aggregate string function) CONCAT(), or else (better for numeric data) use SUM():
SELECT id, SUM(field1), SUM(field2) FROM myTable GROUP BY id
Demo
Below is what I have
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1 + 1 + +
+ 1 + 23 + +
+ 1 + + 1 +
+ 1 + + 33 +
+ 2 + 55 + +
+ 2 + + 2 +
+ 2 + + 23 +
++++++++++++++++++++++++
What I want is
++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1 + 23 + 33 +
+ 2 + 55 + 23 +
++++++++++++++++++++++++
I want to combine the rows (with greatest data) and show data for user in one row against multiple rows like I have in table.
Any idea how to do it?
Note : I don't have any row who have data for all fields. Only 1 data in one row and two or more rows per user.
I tried with
SELECT id, GROUP_CONCAT(MAX(field1)), GROUP_CONCAT(MAX(field2)) from myTable
GROUP BY id;
but its giving error as
Invalid use of group function:
data at sqlfiddle
This question is bit advanced to my earlier question, showing data in one row (from multiple rows)
SELECT id, MAX(field1), MAX(field2) FROM myTable GROUP BY id;
This simple query should do the trick.
SELECT id, MAX(field1), MAX(field2)
FROM myTable
GROUP BY id;
it groups all the rows with the same id and selects the maximum value within each group for each column