sql get data from multiple tables and add the result - mysql

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!

Related

Multiple values in column MYSQL

How my system works is that a user may be assigned to several things.
A column is called group and they may be assigned to several groups, e.g 1,5,8 etc... Is there a way to store them in the same column?
Don't store relations in single column. Normalize your data and introduce new table which will hold references for user group relations like
Table user_groups
user_id group_id
1 1
1 5
1 8
Also have a look at Is storing a delimited list in a database column really that bad?

(MS Access) How to return a field of a different record in a query?

Let's say I have a table with 10 records labeled 1 through 10, and each record contains two fields. I want to create a query that shows me Field 1 of record N with Field 2 of record N+1. For example, the query would show Field 1 of record 3 with Field 2 of record 4. Is this possible?
It is possible not particularily complex.
Given a table tblFoo with FooId as Primary Key and the two additional fields FooText and BarText, the SQL to get the desired results would look like this:
SELECT f1.FooText, f2.BarText
FROM tblFoo AS f1
LEFT JOIN tblFoo AS f2
ON f1.FooID +1 = f2.FooID
While it is simple to implement, performance will no be ideal for large tables because the expression FooId+1 prevents the query engine to use the primary key as index while retrieving the results.

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.

How to get groups of rows in MySQL and Cassandra

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).

Strategy to map multiple lookup tables with system tables?

I have a bunch of lookup tables:
Religion
Country
City
nationality
Currency
etc... approx 80-100 lookup tables.
Now I have system tables where i am centralizing all the fields and values so i can ID all fields, centralize them for reporting and add multi-language to my lookups. So these are tables like:
Form (all forms on system)
Field (all fields on system)
Value (all values for all fields on system)
Form_Field_value (mapping them together)
Translation (maps value and field to multi language)
but the question is how to get all the 80-100 tables data into these Field / Value tables? So it will be like this:
Field table:
id 1 Natioality
id 2 Country
1d 3 city
...
Value table
1d 1 american
id 2 chinese
id 3 rusian
...
field_value
id 1, field_id1, value_id1
id 2, field_id1, value_id2
id 3, field_id1, value_id3
...
Ofcourse i can manually do it but then it defeats the purpose of having those lookup tables. So ideal is to keep these tables in sync. next question is which tables to use for user forms? The lookup tables or the form_fields table?
union select http://dev.mysql.com/doc/refman/5.0/en/union.html