How to get groups of rows in MySQL and Cassandra - mysql

So I have a table that is currently in mysql, but will be transferred to a nosql system soon. So I took out the normalization of the tables, and now there are duplicates of the data, but one of the ids changes in each row, while the rest of the data is constant. All rows are connected through ID A. ID B changes for each row, and the user ID is the same for all of the rows in ID A.
Now I need to grab 2 groups of rows using the user ID. The number of ID B's is variable for every group of A though, so it could have variables number of rows all grouped together by each ID A. So far I have just been displaying one group at a time so I have been selecting based on ID A, now I need to try and grab 2 sets by the user ID...
I can't seem to find a way to do this...although I don't know everything about sql. How can I do this now on mysql? and then on nosql when i move to the system in a bit? Will be happy to answer any further questions.

I think you're saying that the rows have a composite key made up of two columns, id's A and B. On the assumption that I got that right here's how you'd do it in Cassandra (and there are two
approaches).
You could use CQL and declare your table to have two primary keys, A and B, in that order, along with any other columns in your original MySql table.
You could also create a column family whose row key is id A and which will have a column for every unique id B for that id A. The name of the column will be the value of id B and the value of that column will be the value (or serialized values) of the remaining MySQL row values. Note that id B doesn't have to be a String value. For any given value of id A, this will result in a Cassandra column family row with as many columns as there unique id B values for that id A value. This is called the "Dynamic Column Family Pattern".
If you take the first approach, you basically end up doing the second approach under the covers (oversimplification alert).

Related

Multiple rows inside each table row (but only on certain columns)

I'm not quite sure how to word this so I've not managed to find an answer for it!
I want to create a table, where in certain columns there is multiple rows. As shown in the picture
How do I structure this?
An example of what I'm trying to achieve is imagine a table that listed all users in an application. Each row is a user, but I want to also have a sub row for each of the photos that a user may have.
You cannot have multi-valued columns. As they do not satisfy first normal form (For detailed information about 1st Normal form: https://en.wikipedia.org/wiki/First_normal_form)
Let your table have the following columns:
(A,B,C,D)
As per the picture provided by you, I am making the following assumptions:
1) A is the primary key
2) C and D are both are multi-valued and the rows have values like (A1,B1,C1,D1),(A1,B1,C2,D2). That is for single A value we have multiple pairs of C and D.
Do comment if any of the assumption is wrong.
What you can do is make two tables.
TABLE1 (A,B)
TABLE2 (A,C,D)
Where A is the primary key in TABLE1 and foreign key in TABLE2.
As asked for a snippet,you can have tables like these:
Tables

Adding an auto increment sql script

I have a child table named case_parties, that consists of the name and address of each plaintiff and defendant to court cases.
The table columns include:
case_id, which is a foreign key to the parent table
party_type, which has coded field values of either 1 or 2 (1 indicating a plaintiff and 2 indicating a defendant). The caveat is that there is not always just 1 plaintiff and 1 defendant in every court case. Often, there are multiple plaintiffs and or multiple defendants in a single case. There can be anywhere from 1 to 1,000 + plaintiffs and or defendants on any given case. I created a new column, lets call it party_id and SET it with a CONCAT on the case_id and party_type columns. Therefore, matching rows in this column include either all the plaintiffs or all the defendants to a given case_id.
To create a simple unique key for each row, I want to run a script that adds an auto generated incremental number or letter to the end of the matching party_id field. For example, if there are 4 plaintiffs in the same court case, there are now 4 columns with matching party_id field values, with the last character being 1, representing the party is a plaintiff;
I want to add an increment on so each column is unique and the last two digits of the 4 rows would reflect something like this: "1A", "1B", "1C", "1D" or "1-1", "1-2", "1-3", "1-4",...etc. I'm thinking adding incremental numbers might be easier than adding incremental letters. No other column values individually or collectively make for an efficient composite index in this case. I'm seeking assistance with auto incrementing the matching column values and would greatly appreciate any assistance. Thank you.
I would suggest creating a separate table to represent the defendant/plaintiffs and have a type column in there. Then have a primary key on that table with a regular auto-increment.
You can then use that as your ID in the case_parties table (a foreign key) and it will address your issue with uniquely identifying each one.

sql get data from multiple tables and add the result

My MySQL db has a table called catmaster. It contains various "categories", each with a unique id like CAT12. Each "category" is made up of underlying "keywords" and has its own table with 1 column called key_id. So the table CAT12 might have 4 records (keywords) like the following: KEY1 KEY2 KEY3 KEY4. Each of these keywords also has its own table with the same names. Each of these tables has 2 columns, 1 with a date and the other with an integer value, call it inventory.
Is there a way I can write a query that pulls the SUM of the inventory for the underlying keywords (however many there might be) in a given category for a given date?
or is it best to just have my application loop through the records and pull the value for each keyword then sum it itself?
THANKS!

SQL: Fastest Way to Dedupe to Canonical Ids

I have an interesting SQL task and though I would ask the community if anyone knows a fast way to accomplish it. I have 2 slow solutions, but I'm wondering if I am missing something faster.
Here is the task:
Given a list of records in a table, table A, with a column that references the primary key of another table, table B, logically speaking only though this is a MyISAM without foreign keys, we want to dedupe table B, and update table A to use the canonical deduped value from table B, and then delete all but the canonical id records from table B.
This might be easier illustrated via a small example. Lets say table A is a person table, and table B is a city table. Lets also say that there are records in the city table that are duplicates and need deduping. Lets say row 1 and row 2 of table B both refer to Los Angeles.
Then in the person table, we want to update all persons in Los Angeles with city id 2, to have city id 1, and delete the duplicate value from the city table with city id 2.
There may be many such rows representing the duplicated value, not just 2, you get the point. Right now, I am querying out all the cities from the city table, grouping them into equivaslence classes, looping over each equivalence class, nominating the canonical version in this case just choose the first, and performing 2 queries, the updated and the delete:
update person set city_id = $canonical_city_id where city_id in ($list_of_dupes)
Then
delete from city where city_id in ($list_of_dupes) and city_id != $canonical_city_id
I think there may be a faster way since we don't care which id is canonical, it could be the first, the in, or a random, doesn't matter. Can you think of a way to do this whole job in 1 SQL statement? What do you think is the fastest way?

Assign a unique id to 3 tables which have the same column name in mysql

I want to know how to give unique id to 3 tables which have the same column name in mysql.
when inserting a new value the value should be compared with all three table column values and assign a new unique id to the inserted table. Is this possible in mysql.
Thanks
You'll need to create a temporary table that has all the values aggregated together so you can check.
What do you mean by "unique id"? A suitable hash function like SHA1() usually gives you something reasonably unique.