SQL Query - c# database connection (DataGrid) - mysql

I'm trying to get information from 2 tables with a SQL query..
SELECT Num_of_icon, ID_Radar, ID_Observer,
Longitude_Impact_point, Latitude_Impact_point,
Longitude_Impact_point_By_Cutting, Latitude_Impact_point_By_Cutting,
Deviation_In_Meters,
Longitude_Deviation, Latitude_Deviation,
Longitude, Latitude, Azimuth
FROM ShowTable, Observer
ORDER BY Num_of_icon ASC
Num_of_icon is a key in one table.
ID_Observer is a key in the second table and a field in the first table.
The error is:
The field 'ID_Observer' should be show in more than one table.
I dont understand what this error is about.. I know that ID_Observer is showing more than one table, that's why I have a connection between the tables...

You have to qualify a column with tablename.columnname if a column exists in two tables. You also have to link both tables via JOIN:
SELECT Num_of_icon,
ID_Radar,
SHowTable.ID_Observer, --<<< HERE
Longitude_Impact_point,
Latitude_Impact_point,
Longitude_Impact_point_By_Cutting,
Latitude_Impact_point_By_Cutting,
Deviation_In_Meters,
Longitude_Deviation,
Latitude_Deviation,
Longitude,
Latitude,
Azimuth
FROM ShowTable
INNER JOIN Observer
ON ShowTabl.ID_Observer = Observer.ID_Observer --<<< and HERE
ORDER BY Num_of_icon ASC

The query processor does not know which of the ID_Observer fields it should take (and it doesn't make an effort to determine whether they will always be the same value regardless of which table it takes it from). So you'll have to specify this, ie the following will take the ID_Observer value from the Observer table:
"SELECT Num_of_icon,ID_Radar,Observer.ID_Observer,Longitude_Impact_point..."

Related

What is the new table name after joining two tables in sql?

I read the previous posts but I couldn't find one that answered my question.
What would be the name of the table that is made by joining two tables? The reason why I need the name is because I would like to change the column name of the new table using the ALTER TABLE (Table name) RENAME COLUMN (A) to (B). If there is no specified name, how can I name the new table?
ex
SELECT
client_id
,last_name
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name
FROM BUSINESS_CLIENT;
I would like to rename the column to last_name/bus_name instead of last_name
In the case of a query what temporal table a query might create internally is not relevant, because you a making a query and getting data back, it doesn't stay in the database as a table, there is no such table. Unless we make it.
if You want TSQL to change a column name it would affect your union query and I base my answer on Your
'I would like to rename the column to last_name/bus_name instead of last_name'
And think this is what you're looking for. Please correct me if it isn't.
In generic SQL what we're doing is putting a label on both projections that are to be displayed in the same column
SELECT
client_id
,last_name [last_name/ bus_name]
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name [last_name/ bus_name]
FROM BUSINESS_CLIENT;
update, in MySQL notation uses AS and quotes instead of angle brackets
SELECT
client_id
,last_name as "last_name/ bus_name"
FROM INDIVIDUAL_CLIENT
UNION ALL
SELECT
client_id
,bus_name as "last_name/ bus_name"
FROM BUSINESS_CLIENT;

The database reported a syntax error: Duplicate column name 'Product_Number'

It says that The database reported a syntax error:
Duplicate column name 'Product_Number'.
Below is the code:
SELECT *
FROM `df_all_orders_merged_la`
LEFT JOIN `product_database_la`
ON `df_all_orders_merged_la`.`Product_Number` = `product_database_la`.`Product_Number`
WHERE `product_database_la`.`Product_Number` IS NULL;
If both df_all_orders_merged_la and product_database_la contain a column named product_number you will get this error.
What you need to do is to disambiguate your request by using table aliases such as the letters p and m in this rewritten query:
SELECT m.*
FROM `df_all_orders_merged_la` m
LEFT JOIN `product_database_la` p
ON m.`Product_Number` = p.`Product_Number`
WHERE p.`Product_Number` IS NULL;
Here I have used the letters p and m to refer to particular tables in the FROM and JOIN section. Also notice what I did to the SELECT clause: I'm no longer pulling in anything from table p. (But I did this only to get rid of the immediate ambiguity ...)
In general, I recommend that you specify the fields that you actually need in your result, rather than relying on the SELECT * crutch in any of its many forms. Document, in the query, exactly what columns you really need, and which tables they come from:
SELECT m.order_id, p.product_number, p.product_name [...]
Actually you are joining the table and a new temporary table is formed on the basis of Product_Number column in the both parent table so in new table two duplicate column will be formed.
Now since you are calling select * from ... , due to * in select query mysql doesn't know which particular Product_Number column you want to select as having two same name column in this new table is ambigious to it without any specification(like Product_Name of which parent table you want to see) because astrick(*) replace * in query with all the column names in the specified table(without specifying any parent table and it becomes sometimes problematic in case of joins when there is/are column name having same name but different parent tables).
Think about if you are calling just by column name, how does mysql will know which parent table column it should show as there name can be same but there is possibility that content can be different or in just different order and it can't possibly know in this way which particular type column you want. maybe it shows you some random column among duplicates but you wanted some other column having same name!
so the way to select in this type of cases is by specifying the name of all the unique column just by column name(you can specify parent table too but it doesn't matter) without specifying parent table and for duplicate column you will have to call like
select `df_all_orders_merged_la`.`Product_Number,`product_database_la`.`Product_Number` from (... your join query here ...);
i.e specify the parent table of the column from which you want to show like above, again you can give alias to parent tables for your convenience

Is there way to add multiple values to 1 ID in access

I have a table that has Act ID, and another table that has Act ID, percentage complete. This can have multiple entries for different days. I need the sum of the percentage added for the Act ID on the first tableZA.108381.080
First table
Act ID Percent Date
ZA.108381.110 Total from 2 table
ZA.108381.120
ZA.108476.020
ZA.108381.110 25% 5/25/19
ZA.108381.110 75 6/1/19
ZA.108381.120
ZA.108476.020
This would be generally considering not good practice. Your primary key should be uniquely identifiable for that specific table, and any other data related to that key should be stored in separate columns.
However since an answer is not a place for a lecture, if you want to store multiple values in you Act ID column, I would suggest changing your primary key to something more generic "RowID". Then using vba to insert multiple values into this field.
However changing the primary key late in a databases life may cause alot of issues or be difficult. So good luck
Storing calculated values in a table has the disadvantage that these entries may become outdated as the other table is updated. It is preferable to query the tables on the fly to always get uptodate results
SELECT A.ActID, SUM(B.Percentage) AS SumPercent
FROM
table1 A
LEFT JOIN table2 B
ON A.ActID = B.ActID
GROUP BY A.ActID
ORDER BY A.ActID
This query allows you to add additional columns from the first table. If you only need the ActID from table 1, then you can simplify the query, and instead take it from table 2:
SELECT ActID, SUM(Percentage) AS SumPercent
FROM table2
GROUP BY ActID
ORDER BY ActID
If you have spaces other other special characters in a column or table name, you must escape it with []. E.g. [Act ID].
Do not change the IDs in the table. If you want to have the result displayed as the ID merged with the sum, change the query to
SELECT A.ActID & "." & Format(SUM(B.Percentage), "0.000") AS Result
FROM ...
See also: SQL GROUP BY Statement (w3schools)

mysql query using column values as parameter in query phpMyAdmin

I have a query i have been working on trying to get a specific set of data, join the comments in duplicate phone numbers of said data, then join separate tables based on a common field "entry_id" which also happens to be the number on the end of the word custom_ to pull up that table.
table named list and tables containing the values i want to join is custom_entry_id (with entry_id being a field in list in which i need the values of each record to replace the words in order to pull up that specific table) i need entry_id from the beginning part of my query to stick onto the end of the word custom for every value my search returns to get the fields from that custom table designated for that record. so it will have to do some sort of loop i guess? sorry like i said I am at a loss at this point
this is where i am so far:
SELECT * ,
group_concat(comments SEPARATOR '\r\n\r\n') AS comments_combined
FROM list WHERE `status` IN ("SALEA","SALE")
GROUP BY phone_number
//entry_id is included in the * as well as status
// group concat combines the comments if numbers are same
i have also experimented on test data with doing a full outer join which doesnt really exist. i feel if you can solve the other part for me i can do the joining of the data with a query similar to this.
SELECT * FROM test
LEFT JOIN custom_sally ON test.num = custom_sally.num
UNION
SELECT * FROM test
RIGHT JOIN custom_sally ON test.num = custom_sally.num
i would like all of this to appear with every field from my list table in addition to all the fields in the custom_'entry_id' tables for each specific record. I am ok with values being null for records that have different custom fields. so if record 1 has custom fields after the join of hats and trousers and record 2 has socks and shoes i realize that socks and shoes for record 1 will be null and hats and trousers for record 2 will be null.
i am doing all this in phpmyadmin under the SQL tab.
if that is a mistake please advise as well. i am using it because ive only been working with SQl for a few months. from what i read its the rookie tool.
i might be going about this all wrong if so please advise
an example
i query list with my query i get 20,000 rows with columns like status, phone_number, comments, entry_id, name, address, so on.
now i want to join this query with custom fields in another table.
the problem is the custom tables' names are all linked to the entry_id.
so if entry_id is 777 then the custom table fields are custom_777
my database has over 100 custom tables with specials fields for each record depending on its entry_id.
when i query the records I don't know how to join the custom fields that are entry_id specific to the rest of my data.i will pull up some tables and data for a better example
this is the list table:
this is the custom_"entry_id"
Full Outer Join in MySQL
for info on full outer joins.

SQL Query to populate table based on PK of Main Table being joined

Here is my Database structure (basic relations):
I'm attempting to formulate a one-line query that will populate the clients_ID, Job_id, tech_id, & Part_id and return back all the work orders present. Nothing more nothing less.
Thus far I've struggled to generate this Query:
SELECT cli.client_name, tech.tech_name, job.Job_Name, w.wo_id, w.time_started, w.part_id, w.job_id, w.tech_id, w.clients_id, part.Part_name
FROM work_orders as w, technicians as tech, clients as cli, job_types as job, parts_list as part
LEFT JOIN technicians as techy ON tech_id = techy.tech_name
LEFT JOIN parts_list party ON part.part_id = party.Part_Name
LEFT JOIN job_types joby ON job_id = joby.Job_Name
LEFT JOIN clients cliy ON clients_id = cliy.client_name
Apparently, once all the joining happens it does not even populate the correct foreign key values according to their reference.
[some values came out as the actual foreign key id, not even
corresponding value.]
It just goes on about 20-30 times depending on largest row of a table that I have (one of the above).
I only have two work orders created, So ideally it should return just TWO Records, and columns, and fields with correct information. What could I be doing wrong? Haven't been with MySQL too long but am learning as much as I can.
Your join conditions are wrong. Join on tech_id = tech_id, not tech_id = tech_name. Looks like you do this for all your joins, so they all need to be fixed.
I really don't follow the text of your question, so I am basing my answer solely on your query.
Edit
Replying to your comment here. You said you want to "load up" the tech name column. I assume you mean you want tech name to be part of your result set.
The SELECT part of the query is what determines the columns that are in the result set. As long as the table where the column lives is referenced in the FROM/JOIN clauses, you can SELECT any column from that table.
Think of a JOIN statement as a way to "look up" a value in one table based on a value in another table. This is a very simplified definition, but it's a good way to start thinking about it. You want tech name in your result set, so you look it up in the Technicians table, which is where it lives. However, you want to look it up by a value that you have in the Work Orders table. The key (which is actually called a foreign key) that you have in the Work Orders table that relates it to the Technicians table is the tech_id. You use the tech_id to look up the related row in the Technicians table, and by doing so can include any column in that table in your result set.