SQL get column name,row number of a unique value - mysql

I have got some SQL tables, and the structure of them can change. But there is always one unique value somewhere in a table (value's name is "Channel") and I always need to get the column name and the row number of this value in a current table ( to further get all values in this column with a row number greater as of "Channel") . How could the code for that look like?
Thanks,
Jeff

Related

MS Access loop query for every row on a table

I'm not very experienced with MS Access and I'm trying to loop through every record on a table (tbl_jobs_main) and run this query against it.
SELECT division.[Department]
FROM division
WHERE tbl_jobs_main.[org1] = division.[ORG1];
My hope is that the new column "Department" in the tbl_jobs_main table can be auto filled from the division table with the Department values by matching the ORG1 values for each row. To clarify, the ORG1 values in the division table are unique and the org1 values in tbl_jobs_main are duplicated.
Any assistance would be greatly appreciated.
Edit:
(Sorry, not enough rep to post images on this profile.)
Example data in Division table:
https://i.stack.imgur.com/LYDh3.png
Example data in tbl_jobs_main table:
https://i.stack.imgur.com/zIel9.png
I need for the Department column to populate with the corresponding Department in the Division table based on the matching ORG1 value.
Ideally I could run something so the tbl_jobs_main becomes this:
https://i.stack.imgur.com/VU1ko.png
Whilst you can loop through tbl_jobs_main and update the Department based on data in the Division table, you can also use an update Query. The SQL will look like:
UPDATE tbl_jobs_main AS J INNER JOIN Division AS D
ON D.Org1=J.Org1
SET J.Department=D.Department
However, I am not sure why you would want to do this. If the name of "Dept1" in the division table changes, you then have to re-update tbl_jobs_main to reflect this. Far better to use Access as a relational database, where you store the department information in the Division table, and link the tables.
Regards,

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.

Adding a column of data

Not been able to find something that matches what I am setting out to teach myself, so here goes:
I have added a column (called status) to an existing table (called fruit). All values in this new column are currently null. Other columns in this table are id (primary key int(11) ) and fruitname.
My question is this:
Is there a command I can use to populate this column in one go? Or do I need to update each row one by one?
Ideally I am looking for something that populates columns in the same way insert does to rows. Something where I can specify the table and column name and then list the values to fill down.
If you want the new column status to have same value for all records like Fresh then you can do it in one go like
update fruit set status = 'fresh'
Else, you have no other way than performing an UPDATE row by row and populate the status column value for each fruit record.

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