Order by in full text search in mysql does not work - mysql

I need to sort my result in full text search. this is my code
'SELECT presentations.id, ' +
'presentations.72_png , ' +
'presentations.topic_id, ' +
'presentations.name, ' +
'presentations.presenter_id , ' +
'presentations.synopsis , ' +
'presentations.tags , ' +
'presentations.teaching_guide_text , ' +
'presentations.video_type, ' +
'presenters. first_name, ' +
'presenters.last_name, ' +
'presenters.title, ' +
'presenters.company, ' +
'MATCH (presentations.name) AGAINST (%s) AS rel1 ,' +
'MATCH (presentations.synopsis) AGAINST (%s) AS rel2,' +
'MATCH (presentations.tags) AGAINST (%s) AS rel3,' +
'MATCH (presentations.teaching_guide_text) AGAINST (%s) AS rel4,' +
'MATCH (presenters.first_name,presenters.last_name) AGAINST (%s) AS rel5 ,' +
'MATCH (presenters.title) AGAINST (%s) AS rel6 ,' +
'MATCH (presenters.company) AGAINST (%s) AS rel7 , ' +
'MATCH(presentations.search_leadership) AGAINST (%s) AS rel8 ,' +
'MATCH(presentations.search_business) AGAINST (%s) AS rel9 ,' +
'MATCH(presentations.search_academic) AGAINST (%s) AS rel10 ,' +
'MATCH(presentations.search_industry) AGAINST (%s) AS rel11 ' +
'FROM presentations ' +
'INNER JOIN presenters ON ' +
'(presentations.presenter_id = presenters.id) ' +
'WHERE presentations.deleted=0 AND ' +
'presentations.published=1 AND ' +
'(MATCH (presentations.name, ' +
'presentations.synopsis , ' +
'presentations.tags , ' +
'presentations.teaching_guide_text) AGAINST (%s) OR ' +
'MATCH (presenters.first_name , ' +
'presenters.last_name , ' +
'presenters.title , ' +
'presenters.company) AGAINST (%s) OR ' +
'MATCH (presentations.search_leadership, ' +
'presentations.search_business , ' +
'presentations.search_academic , ' +
'presentations.search_industry) AGAINST (%s)) AND ' +
'academic_only = 0 ' +
'ORDER BY (rel6 * 1000)+ (rel7 * 1000) + (rel8 * 90) + (rel9 * 80) + (rel10 * 70) + (rel11 * 60) DESC ',
[word, word, word, word, word, word, word, word, word, word,
word, word, word, word, ])
and this code doesn't give me sort order. if I remove the "'ORDER BY (rel6 * 1000)+ (rel7 * 1000) + (rel8 * 90) + (rel9 * 80) + (rel10 * 70) + (rel11 * 60) DESC '" it also give the same result as previous. what's wrong with my SQL code?

Related

How do I concatenate names with varying characters in between based on Conditions?

I'm very very new to jQuery and I'm trying to create a query to use that creates a greeting line for use in html that varies depending on whether certain columns are empty
The data looks like this:
forename (actual)
surname (actual)
forename (actual)_1
surname (actual)_1
forename (actual)_2
surname (actual)_2
John
Smith
Judith
Jones
Brian
Jones
Ted
Jones
Phil
Daniels
Chris
Daniels
I would want to return a greeting that looked like this for each line
Dear John Smith
Dear Judith Jones, Brian Jones and Ted Jones
Dear Phil Daniels and Chris Daniels
So basically the code would need to check how many names are present, and put ',' or 'and' between the last two
The code I've created so far is this, based on some old variables used in a program we used pre-javascript
if (sMultipleNames.length >= 1)
sMultipleNames3 = record.fields["forename (actual)"] + ' ' +
record.fields["surname (actual)"] + ', ' +
record.fields["forename (actual)_1"] + ' ' +
record.fields["surname (actual)_1"] + ', ' +
record.fields["forename (actual)_2"] + ' ' +
record.fields["surname (actual)_2"] + ' and ' +
record.fields["forename (actual)_3"] + ' ' +
record.fields["surname (actual)_3"];
else if (sMultipleNames.length >= 1)
sMultipleNames2 = record.fields["forename (actual)"] + ' ' +
record.fields["surname (actual)"] + ', ' +
record.fields["forename (actual)_1"] + ' ' +
record.fields["surname (actual)_1"] + ' and ' +
record.fields["forename (actual)_2"] + ' ' +
record.fields["surname (actual)_2"];
else if (sMultipleNames.length >= 1)
record.fields["forename (actual)"] + ' ' +
record.fields["surname (actual)"] + ' and ' +
record.fields["forename (actual)_1"] + ' ' +
record.fields["surname (actual)_1"];
else(sMultipleNames)
record.fields["forename (actual)"] + ' ' +
record.fields["surname (actual)"]
}
But I have no idea how to execute it all in one query!
Apologies if this is an incredible easy query, I've tried w3 and other stack overflow posts but they are either way more complicated than I think I need, or I just can't parse the examples into my own requirements!
There could also be more names in the data, up to 10, so if there was a way of doing it ad infinitum that'd be great. I presume it can be done with a switch but I've no idea what the initial expression should be, perhaps a way of checking whether cells are empty and returning the greeting based on that?
I'm not sure what sMultipleNames actually is.
Assume sMultipleNames is the number of names;
Try this for loop
let ans = record.fields["forename (actual)"] + ' ' + record.fields["surname (actual)"]
for (let i = 1; i < sMultipleName; ++i ) {
if (i === sMultipleName - 1) {
ans = ans + ', ' + record.fields[`forename (actual)_${i}`] + ' ' + record.fields[`surname (actual)_${i}`];
} else {
ans = ans + ' and' + record.fields[`forename (actual)_${i}`] + ' ' + record.fields[`surname (actual)_${i}`];
}
}
the usage `string` is called template strings
you can check it here
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Why sum() function isn't giving the exact value in this case?

select s_id, s_f_name, s_l_name, s_roll_no, s_class,
SUM(mid_1_english + mid_2_english)/2 as mid_english,
SUM(mid_1_mathematics + mid_2_mathematics)/2 as mid_mathematics,
semester_final_english,
semester_final_mathematics,
SUM((mid_1_english + mid_2_english)/2 + semester_final_english) as total_english,
SUM((mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total_mathematics,
SUM((mid_1_mathematics + mid_2_mathematics)/2 + (mid_1_english + mid_2_english)/2 + semester_final_mathematics + semester_final_mathematics) as total
from elaborate_term_result_sheet
group by s_f_name, s_l_name
order by s_roll_no;
After running this query created, it does show the out put, but value is getting a bit fluctuated
mid_1_english + mid_2_english = mid_english
mid_1_mathematics + mid_2_mathematics = mid_mathematics
mid_english + semester_final_english = total_english
mid_mathematics + semester_final_mathematics = total_mathematics
total = total_english + total_mathematics
select s_id, s_f_name, s_l_name, s_roll_no, s_class,
SUM(mid_1_english + mid_2_english)/2 as mid_english,
SUM(mid_1_mathematics + mid_2_mathematics)/2 as mid_mathematics,
semester_final_english,
semester_final_mathematics,
SUM((mid_1_english + mid_2_english)/2 + semester_final_english) as total_english,
SUM((mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total_mathematics,
SUM((mid_1_english + mid_2_english)/2 + semester_final_english + (mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total from result_sheet_1 group by s_f_name, s_l_name order by s_roll_no;
the only change that gave the right output is
SUM((mid_1_english + mid_2_english)/2 + semester_final_english + (mid_1_mathematics + mid_2_mathematics)/2 + semester_final_mathematics) as total
the snapshot of the output of this query

Delphi XE10.x FireDAC - Error lost connection to MySQL server during query

I am building this simple query using following code:
FDQuery1.SQL.Text := 'INSERT INTO album SET customer_id=' + Chr(39) + IntToStr(CustomerID) + Chr(39) + ',lab_id=' + Chr(39) + IntToStr(LabID) + Chr(39) +
', album_name_c = ' + Chr(39) + ProjectFolderName + Chr(39) +
', album_name_s = ' + Chr(39) + ProjectFolderNameOnServer + Chr(39) +
' ,album_size=' + Chr(39) + txtAlbumSize.Text + Chr(39) +
', album_paper=' + Chr(39) + txtPaperFinish.Text + Chr(39) +
', album_cover=' + Chr(39) + txtCover.Text + Chr(39) +
', album_binding=' + Chr(39) + txtBinding.Text + Chr(39) +
', album_coating=' + Chr(39) + txtCoating.Text + Chr(39) +
', starts_from=' + Chr(39) + BoolToStr(chkRight.Checked) + Chr(39) +
'; SELECT LAST_INSERT_ID() AS RecID;';
But when I call FDQuery1.OpenOrExecute I get error message as stated in subject of this post.
Current query length is around 299 characters.
I read somewhere on internet that the Query length should be less than 255 characters, so I have also tried to shorten the query but keep its length below 255 characters.
But still I get the same error message.
What wrong am I doing here please guide me.
TIA
Yogi Yang
Ok finally I found the actual problem and solved it...
It so happens that the dumb FireDAC's Query component does not support query in the format stated in original post. I had to build traditional Insert query as per SQL norms.
Though query with SET keyword is supported by MySQL and I have been using it in my PHP projects so there is not question of the query being wrong.
Ok what I did with suggestion/help of other developers in another forum was build a parameterized query and use it like below:
FDQuery1.SQL.Text := 'INSERT INTO album(customer_id, album_name_c, album_name_s, album_size, album_paper, album_cover, album_binding, album_coating, starts_from, total_files, lab_id, upload_date, album_uploaded) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?);';
FDQuery1.Params.Items[0].AsInteger := CustomerID;
FDQuery1.Params.Items[1].AsString := ProjectFolderName;
FDQuery1.Params.Items[2].AsString := ProjectFolderNameOnServer;
FDQuery1.Params.Items[3].AsString := AlbumSize;
FDQuery1.Params.Items[4].AsString := PaperFinish;
FDQuery1.Params.Items[5].AsString := Cover;
FDQuery1.Params.Items[6].AsString := Binding;
FDQuery1.Params.Items[7].AsString := Coating;
if chkLeft.Checked then
FDQuery1.Params.Items[8].AsString := '1'
else
FDQuery1.Params.Items[8].AsString := '0';
if chkRight.Checked then
FDQuery1.Params.Items[8].AsString := '2'
else
FDQuery1.Params.Items[8].AsString := '0';
FDQuery1.Params.Items[9].AsInteger := lstFiles.Count;
FDQuery1.Params.Items[10].AsInteger := LabID;
FDQuery1.Params.Items[11].AsDate := Now;
FDQuery1.Params.Items[12].AsString := '1';
FDQuery1.Execute;
Thanks to this parameterized query I did not have to add single quotes to varchar and data fields data using Chr(39).
I hope this will help other developers solve similar problems that they may face.
Regards,
Yogi Yang

SUM function in sql returns miscalculated value

Good day! I've been running around in circles for finding solution for this problem, i have summed my columns
SELECT sum(sp1)
, sum(sp2)
, sum(sp3)
, sum(sp4)
, sum(fsg)
, sum(fgg)
, sum(ffg)
, sum(fsf)
, sum(fgf)
, sum(fff)
, sum(g1)
, sum(g2)
, sum(g3)
, sum(gb35)
, sum(gb452mm)
, sum(gb453mm)
, sum(gb454mm)
, (sum(sp1) + sum(sp2) + sum(sp3) + sum(sp4) + sum(fsg) + sum(fgg) + sum(ffg) + sum(fsg) + sum(fgf)
+ sum(fff) + sum(g1) + sum(g2) + sum(g3) + sum(gb35) + sum(gb452mm) + sum(gb453mm) + sum(gb454mm)) total
, monthx
, yearx
, monthval
FROM tbl_incoming
WHERE yearx = '" & AnnualSelectYearFrm.ComboBox3.Text & "'
GROUP
BY monthx
, yearx
, monthval
ORDER
BY monthval
where the total(sum of all columns) of the colums are miscalculated, i think some of it are not in the computation, i've been wondering why and what is causing this. Please help.
try this:
(spa1+spa2+spa3+spa4)
else try this also:
sum(spa1+spa2)
instead of this:
(sum(sp1) + sum(sp2) + sum(sp3) + sum(sp4) + sum(fsg) + sum(fgg) + sum(ffg) + sum(fsg) + sum(fgf)
+ sum(fff) + sum(g1) + sum(g2) + sum(g3) + sum(gb35) + sum(gb452mm) + sum(gb453mm) + sum(gb454mm)) total

Script table schema using tsql

I am writing code to script CREATE TABLE statements for all tables across all databases on a server.The code below will work for a specific database. Would like some ideas on how to modify it, so it can script table schema for all the tables.
select 'create table [' + so.name + '] (' + o.list + ')' + CASE WHEN tc.Constraint_Name IS NULL THEN '' ELSE 'ALTER TABLE ' + so.Name + ' ADD CONSTRAINT ' + tc.Constraint_Name + ' PRIMARY KEY ' + ' (' + LEFT(j.List, Len(j.List)-1) + ')' END
from sysobjects so
cross apply
(SELECT
' ['+column_name+'] ' +
data_type + case data_type
when 'sql_variant' then ''
when 'text' then ''
when 'decimal' then '(' + cast(numeric_precision_radix as varchar) + ', ' + cast(numeric_scale as varchar) + ')'
else coalesce('('+case when character_maximum_length = -1 then 'MAX' else cast(character_maximum_length as varchar) end +')','') end + ' ' +
case when exists (
select id from syscolumns
where object_name(id)=so.name
and name=column_name
and columnproperty(id,name,'IsIdentity') = 1
) then
'IDENTITY(' +
cast(ident_seed(so.name) as varchar) + ',' +
cast(ident_incr(so.name) as varchar) + ')'
else ''
end + ' ' +
(case when IS_NULLABLE = 'No' then 'NOT ' else '' end ) + 'NULL ' +
case when information_schema.columns.COLUMN_DEFAULT IS NOT NULL THEN 'DEFAULT '+ information_schema.columns.COLUMN_DEFAULT ELSE '' END + ', '
from information_schema.columns where table_name = so.name
order by ordinal_position
FOR XML PATH('')) o (list)
left join
information_schema.table_constraints tc
on tc.Table_name = so.Name
AND tc.Constraint_Type = 'PRIMARY KEY'
cross apply
(select '[' + Column_Name + '], '
FROM information_schema.key_column_usage kcu
WHERE kcu.Constraint_Name = tc.Constraint_Name
ORDER BY
ORDINAL_POSITION
FOR XML PATH('')) j (list)
where xtype = 'U'
AND name NOT IN ('dtproperties')
Since you seem determined on TSQL, a quick and dirty solution would be to use the (undocumented) sp_foreachdb stored procedure. That will give you the name of every database on the sql instance, so you would just USE each database in turn and run TSQL that you posted (after escaping all the single qoutes in it).
Heres a shortened example:
EXECUTE sp_msforeachdb 'USE [?]
select ''create table ['' + so.name + ''] ''
from sysobjects so
-- the rest of your escaped TSQL goes here...
-- then finish with a closing quote:
'