I am trying to create a simple filtering of records with bit operations by using this manual: https://dev.mysql.com/doc/refman/8.0/en/bit-functions.html
I have four properties that are defined and based on certain content:
Filter 1 (field1) gets the value 1 (binary 1)
Filter 2 (field2) gets the value 2 (binary 10)
Filter 3 (field3) gets the value 4 (binary 100)
Filter 4 (field4) gets the value 8 (binary 1000)
I set the values with an update:
UPDATE table SET filter = 1 where field1 = "a";
UPDATE table SET filter = filter|2 where field2 = "b";
UPDATE table SET filter = filter|4 where field3 = "c";
UPDATE table SET filter = filter|8 where field4 = "d";
Now the column is filled for the different properties. I now have values between 0 (no property applies) and 15 (all properties apply).
How do I manage to use them now? If I want to use e.g. the filters 1,2 and 4, I get with:
select * from table where filter = 1|2|8;
I get the value "11". But actually, "15" should also match, since all four properties are applied here.
I had no success with this, too:
select * from table where filter & (1|2|8);
Can someone help me? Or am I completely wrong?
Try WHERE (filter & (1|2|8)) = (1|2|8).
But please be aware that this bitmasking approach can't exploit indexes, so it will scale up poorly to megarow tables.
Related
I need create a Delta Report which is based on the two data sets i.e DataSet1 and DataSet2.
I need take DataSet2 as reference if there is any change in any field value in DataSet1 I need change the color of the text box.
can anybody help me in this I wrote some code but its throwing Error.
=IIF(Fields!CIF.value, "DataSet1" = Fields!CIF.value, "DataSet2" AND Fields!Account_ID.value,"DataSet1" = Fields!Account_ID.value,"DataSet2",
IIF( Fields!Account_balance.value, "DataSet1" <> Fields!Account_balance.value, "DataSet2","Yellow","Transparent"),"Transparent","Transparent")
You can't mix datasets.
This will only work if your datasets each only have one row of data. Otherwise you'll need to figure out how to isolate the value you need.
=IIF(MAX(Fields!CIF.value, "DataSet1") = MAX(Fields!CIF.value, "DataSet2") AND MAX(Fields!Account_ID.value, "DataSet1") = MAX(Fields!Account_ID.value, "DataSet2"),
IIF(MAX(Fields!Account_balance.value, "DataSet1") <> MAX(Fields!Account_balance.value, "DataSet2"), "Yellow", "Transparent"), "Transparent")
Usually you'd have one dataset in a table and then look up the corresponding value in the other dataset.
If your CIF and Account ID are the common identifiers and you want to compate the Account Balances of each, I would base the table on dataset 1 and then bring in the value from dataset2 to compare. Combine the CIF and Account ID into a single text string for comparison.
Your color expression would then be something like:
=Lookup(Fields!CIF.value & "|" & Fields!Account_ID.value, Fields!CIF.value & "|" & Fields!Account_ID.value, Account_balance.value, "DataSet2")
SSRS:
Use Lookup to retrieve the value from the specified dataset for a
name-value pair where there is a 1-to-1 relationship. For example, for
an ID field in a table, you can use Lookup to retrieve the
corresponding Name field from a dataset that is not bound to the data
region.
Another approach would be to do this in a stored procedure and return the results as one dataset. For example, assume I have 10 fields in the tables. I can create a hash of the data for each record:
select
CASE WHEN chkHash2 = chkHash1 THEN 0 ELSE 1 END as 'ChgFlag'
,a.*
FROM
(
select
HASHBYTES('md5', t2.Field1 + t2.Field2 + ... + t2.Field10) as 'chkHash2'
,HASHBYTES('md5', t1.Field1 + t1.Field2 + ... + t1.Field10) as 'chkHash1'
,t1.Field1, t1.Field2, ... t1.Field10
From Table2 t2
LEFT JOIN Table1 t1 on t1.ID = t2.ID
) a
Big Assumption ... both tables have an ID that is unique and related.
This will give you a result set of Table1 (dataset1) fields and a flag that will tell you when any field has changed from Table2 (dataset2).
Depending on your application, this could be easier and faster. I always try to do the WORK in SQL as opposed to SSRS.
What I am trying to do is reduce the time needed to aggregate data by producing a roll-up table of sorts. When I insert a record, an after insert trigger is fired which will update the correct row. I would update all of the columns of the roll-up table if I need to, but since there are 25 columns in the table and each insert will only update 2 of them, I would rather be able to dynamically select the columns to update. My current update statement in the after insert trigger looks similar to this:
update peek_at_chu.organization_data_state_log odsl
inner join ( select
lookUpID as org_data_lookup,
i.interval_id,
peek_at_chu.Get_Time_Durration_In_Interval1('s', new.start_time, new.end_time, i.start_time, i.end_time) as time_in_int,
new.phone_state_id
from
(peek_at_chu.interval_info i
join peek_at_chu.interval_step int_s on i.interval_step_id = int_s.interval_step_id)) as usl on odsl.org_date_lookup_id = usl.org_data_lookup
and odsl.interval_id = usl.interval_id
set
total_seconds = total_seconds + usl.time_in_int,
case new.phone_state_id
when 2 then
available_seconds = available_seconds + time_in_int
end;
In this, lookUpID is a variable previously declared in the trigger. The field that will dictate which field of the roll-up table to update is new.phone_state_id. The phone_state_id's are not consistent, that is some numbers are skipped in this table, so an update based on column number is out the window unless I create a mapping.
The case option throws an error but I am hoping to use something similar to that instead of 25 if statements if I can.
You have to update all the columns, but use a conditional to determine whether to give it a new value or keep the old value:
set total_seconds = total_seconds + usl.time_in_int,
available_seconds = IF(new.phone_state_id = 2, available_seconds + time_in_int, available_seconds)
Repeat the pattern in the last line for all the other columns that need to be updated conditionally.
SQL:
$mysqli->query("UPDATE results
SET result_value = '".$row[0]['logo_value']."'
WHERE logo_id = '".$mysqli->real_escape_string($_GET['logo_id'])."'
AND user_id = '".$user_data[0]['user_id']."'");
This results table also contains result_tries I'd like to fetch before doing update, so I can use it to modify result_value... Is there a way to do it in a single shot instead of first doing select and than doing update?
Is this possible?
Basically:
UPDATE results SET result_value = result_value + $row[0][logo_value]
for just a simple addition. You CAN use existing fields in the record being updated as part of the update, so if you don't want just addition, there's not too many limits on what logic you can use instead of just x = x + y.
The query:
$consulta = "UPDATE `list`
SET `pos` = $pos
WHERE `id_item` IN (SELECT id_item
FROM lists
WHERE pos = '$item'
ORDER BY pos DESC
LIMIT 1)
AND id_usuario = '$us'
AND id_list = '$id_pl'";
The thing is, this query is inside a foreach, and it wants to update the order of the items in a list. Before I had it like this:
$consulta = "UPDATE `list`
SET `pos` = $pos
WHERE `$pos` = '$item'
AND id_usuario = '$us'
AND id_list = '$id_pl'";
But when I update pos 2 -> 1, and then 1 -> 2, the result is two times 2 and no 1...
Is there a solution for this query?
Renumbering the items in a list is tricky. When you renumber the items in the list using multiple separate SQL statements, it is even trickier.
Your inner sub-select statement also is not properly constrained. You need an extra condition such as:
AND id_list = '$id_pl'
There are probably many ways to do this, but the one that may be simplest follows. I'm assuming that:
the unshown foreach loop generates $pos values in the desired sequence (1, 2, ...)
the value of $id_pl is constant for the loop
the foreach loop gives values for $us and $item for each iteration
the combination of $id_pl, $us, and $item uniquely identifies a row in the list table
there aren't more than 100 pos values to worry about
you are able to use an explicit transaction around the statement sequence
The suggested solution has two stages:
Allocate 100 + pos to each row to place it in its new position
Subtract 100 from each pos
This technique avoids any complicated issues about whether rows that have had there position adjusted are reread by the same query.
Inside the loop:
foreach ...
...$pos, $item, $us...
UPDATE list
SET pos = $pos + 100
WHERE id_item = '$item'
AND id_usuario = '$us'
AND id_list = '$id_pl'
AND pos < 100
end foreach
UPDATE list
SET pos = pos - 100
WHERE id__list = '$id_pl';
If you don't know the size of the lists, you could assign negative pos values in the loop and convert to positive after the loop, or any of a number of other equivalent mappings. The key is to update the table so that the new pos numbers in the loop are disjoint from the old numbers, and then adjust the new values after the loop.
Alternative techniques create a temporary table that maps the old numbers to the new and then executes a single UPDATE statement that changes the old pos value to the new for all rows in a single operation. This is probably more efficient, especially if the mapping table can be generated as a query, but that depends on whether the renumbering is algorithmic. The technique shown, albeit somewhat clumsy, can be made to work for arbitrary renumberings.
I have a MySql db with innoDB tables. Very simplified this is how two tables are layed out:
Table A:
controlID(PK)
controlText
Table B:
controlOptionID(pk)
controlID(FK to table A)
controlOptionType
controlOptionValue
So many controlOptions(table B) can reference one control(giving that control multiple options). But for each option two rows are made in table B: one row with controlOptionType = "linkToCreator" and controlOptionValue = (an ID to the template it was made from*). And the other row type = "optionSelected" and value = "true"(or false).
= its a pretty complicated setup, but basically instead of set columns we are making dynamic ones by means of the type being what the column would have been called. So I couldnt link to the template with FK.
So now I need to select every control(which will have 2 controlOptions linking to it) where the one controlOptionValue value is true or false(depending on what i need) and the other controlOptionValue is an text ID that I specify.
What I think is the best way to do it is a
SELECT * FROM tableB WHERE controlOptionType = 'linkToCreator'
Then do a loop over that result set saying:
SELECT * FROM tableB WHERE tableB.controlID = (the controlID in this iterations row) AND tableB.controlValue = 'true'
But maybe thatls really inefficient, and either way I have no clue how to do that. It would be great if I could get a single query(i.e. not using stored procedures) that I specified templateID and true or false and it gave me a row result if it didn't find anything.
BTW this is for a search in our application with will need to go through TONS of rows so performance is paramount. And yes, I know the setup isnt the greatest...
Thanks :D
Like this?
SELECT * FROM tableA AS A
LEFT JOIN tableB AS ctrl1 ON (A.controlID = ctrl1.controlID AND ctrl1.controlOptionType = ? AND ctrl1.controlOptionValue = ?)
LEFT JOIN tableB AS ctrl2 ON (A.controlID = ctrl2.controlID AND ctrl2.controlOptionType = ? AND ctrl2.controlOptionValue = ?)
Try this:
SELECT *
FROM Table_A
LEFT JOIN Table_B
ON Table_A.ControlID = Table_B.ControlID
WHERE Table_A.controlOptionType = 'linkToCreator