Combine all rows of a table and add to another - mysql

I have interim results table where each question in a quiz is scored individually. Once that's done, I want to add up certain fields of all rows into a single row and then add the row to a adminresults table. I am able to do this in a two steps but not sure how to combine it into a single query.
This is my code to combine the specific fields from the interimr table:
select sum(TotalScore),
sum(ActualScore),
GROUP_CONCAT(Scorebreakdown),
GROUP_CONCAT(Feedback)
From interimr
This is my code to enter specific fields into the adminresults table:
INSERT INTO `adminresults`(`TotalScore`, `ActualScore`, `Scorebreakdown`, `Feedback`)
VALUES (50,40, 'First try', 'First try')
I have hyperlinked a pics of the structure my tables. Thanks

I am assuming that by "add to" you mean insert into. If you mean "update a single row in", then the syntax is a bit different.
Just use insert . . . select syntax:
INSERT INTO `adminresults`(`TotalScore`, `ActualScore`, `Scorebreakdown`, `Feedback`)
select sum(TotalScore), sum(ActualScore), GROUP_CONCAT(Scorebreakdown), GROUP_CONCAT(Feedback)
From interimr;
Note: you might want additional information such as the date/time of the insertion, but that is outside the scope of your question.

Related

How to check whether a value exists among a set of values in a record in mysql

I am using MySQL in such a way that the user enters a date and flights appear on that day.I was too lazy to put many dates so what I did was made certain flights run on certain days.
The table planes contains many columns out of which dtd(column containing many days of type varchar(50)) .
One record(in dtd) is Monday, Tuesday
The user enters the date 2018-12-03 which is a Monday.
My problem is how do I extract only the record which has Monday?(like the monday,Tuesday one)??
I tried select.....where instr(day(2018-12-03),dtd)<>0 but I realised that instr gives 0 in 2 cases
1. the character(s) is not present in the record or
2.occurence is at the beginning.
Like instr(h,hello) and instr(a,hello) gives 0.
Please help
I suggest that you use the LIKE operator.
For more info about it please check this link: SQL Like Operator
To solve your problem, you can try the following:
SELECT ...
WHERE dtd LIKE CONCAT('%', day(2018-12-03), '%');
In this case, you will get all the rows of the table, where the value of the field dtd contains the value of day(2018-12-03).
Hope this helps!

FIND_IN_SET() not working for single value

I have table named post in which there is a column called visible_user_ids in which comma separated values are there. When I display the post by respective user then I used the FIND_IN_SET().
e.g. FIND_IN_SET('8','visible_user_ids') it shows the all the records from post table for user id 8 when visible_user_ids column contains comma separated user ids such as
3,5,8
8,5
8,1,12
etc.
but when visible_user_ids column contain only one value i.e. 8 then it does not display the post table record.
Please suggest a solution. Whether FIND_IN_SET() works for single value?
Instead of
FIND_IN_SET('8','visible_user_ids')
Try this
FIND_IN_SET('8',visible_user_ids)

Automatically calculate common unit values after entry into Access table

I have a basic Table called ReceiveReport which contains the fallowing fields ID, Units, GrossWt, NetWt, TareWt. I allow the user to select his units (lbs, kgs) afterwards he has to enter gross/net/tare weight into the fields. I want the fields to update them self depending on the unit field. I tried using the After Insert Macro, but I couldn't use SetField which made me confused about how to update the field.
I want to do something fairly basic.
iff([units] = "lbs", [field]*2.2046, Do Nothing )
am I going at this the wrong way?
You could update the value in a Before Change macro like the following. The approach I chose leaves the [units] and [weight_entered] values intact and updates a separate field named [weight_kg], but you could also overwrite the values that were originally entered if that was your preference.

concatenate using Allen Browne's example

I am using the 'concatenate related' module created by Allen Browne to concatenate rows into a single field. At first I had a lookup field at the table level and later realized this is not a good approach. So I deleted the lookup column and instead made a query for selecting values from the lookup table on my form and then store that value as a number in the table.
The module works when I concatenate the values but it is listing the number (id) whereas I would like the actual description (i.e. 1 = Red, 2 = Blue, etc.)
My SQL query code is as follows:
SELECT DISTINCT
tblCompany.JobID,
concatrelated("type","tblMonitor","JobID = " & [jobID]) AS Expr1
FROM tblCompany;
I would like "type" to display the description instead of the number. I know if I store my lookup value as text instead of number it will work. But for efficiency it seems the number should be stored in the table and then query for the description when you need it....or maybe text is fine??? I'm guessing I would need to add the lookup table to this query. I have tried but with no luck so far.
Create a query which joins tblMonitor with the table which holds the type description field. Then use that query with ConcatRelated.
ConcatRelated("type_descriptn","YourQuery","JobID = " & [jobID])

MYSQL search table, bit fields

I have a table with rows and where one field is a bit-value with 7 digits.
Suppose I have a procedure where I want to select all rows where this bit field equals '0101010', this is easily done by select * where .... and so on.
But: how do I do if I want to allow one/multiple digits of the digits to be either 1 Or 0, i.e I want to get all rows where the bitfield has an entry on the form 1001*1* where the * can be either 1 or 0. So, in this case I would like all entries where the bit field is 1001010, 1001011, 1001110 or 1001111.
select * from TABLE where bit_field in (1001010, 1001011, 1001110, 1001111) would probably work in this example, but if I want to use only the string '1001*1*' as input to the procedure, what then?
.
Any help is very appreciated.
Thanks,
Niklas
Edit: I've tried this: select * from table where field like bit'\\\0'; for getting all entries of the form **0, but that didn't work...
Edit2: It turned out it vas a bit-field, not binary... problem still remain though.
Not a direct answer to your question, per se', but an alternative approach. You mentioned that you didn't want to convert to individual columns because of legacy code. If you do want individual columns and the only thing holding you back is the legacy code, consider the following.
You could add columns for the options and use insert/update triggers to populate them OR you could create a view that splits the bits into separate columns. For new development, you can code to the new columns. Over time, as you modify legacy code you can change it to the new approach. When all the "read" legacy code has been changed, the last step is to change the "write" code to use the new columns rather than the bit column and remove the triggers.
I have a SQL Fiddle demonstrating this here. Note that I only included an insert trigger for brevity.