How do you SELECT from Two Tables in MySql - c#

I have two MySql tables which I need to select a column from one, and where the results are used to select from another table. I know how to do it as two different select statements. However, I believe I can do it as a single statement but have no idea how.
Table one has two columns the second column has values which are also found in table two. I need to select all rows in table two which has the same values as those found in table one and where another column value is 0.
Any ideas how to go about doing this?

Use Join On tables to get columns form both table using query as
SELECT column_list
FROM table_1
LEFT JOIN table_2 ON
table_1.column = table_2.column;

Try to use Join query
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Related

SQL Server: returning combined data from two tables using c#

I hope someone can help me. My problem is that I have two tables in my database.
I want to return the IDs from tableA and tableB with one stored procedure.
Therefore I have two selects:
SELECT
id, 'WE'
FROM
tableX
WHERE
value= 'x'
SELECT
id, 'WT'
FROM
tableY
WHERE
value= 'y'
How can I combine them so I get only one object in my C# code?
Any example? Did not find a sample code for this case. Somehow with a cursor?
It is old code, so no Entity Framework available.
Thanks
The UNION statement can do this for you
SELECT
id, 'WE' from
tableX
where value= 'x'
UNION
SELECT
id, 'WT' from
tableY
where value= 'y'
UNION ALL performs a similar function, but will not filter out identical rows from the UNION result. In this case, you specify different literal values for the second column, so UNION and UNION ALL will produce the same set.
And if the tables do not have the same amount of columns, you can use a dummy column:
SELECT Id, we, them from table x where value = 'x'
UNION
SELECT Id, we, null as them from table y where value ='y'

How to copy data from one column in certain table to same named column in another table in SQL Server

I have two SQL Server tables sub_aminer_author2paper and sub_aminer_paper, and there is a common column pid.
Another column aid exists in only one table i.e. sub_aminer_author2paper.
Now I have to copy column aid from table sub_aminer_author2paper to newly created column aid in the table sub_aminer_paper, whereas the column pid in table sub_aminer_paper should match the pid in table sub_aminer_author2paper
I have tried this query as:
insert into sub_aminer_paper (sub_aminer_paper.aid)
(select sub_aminer_author2paper.aid
from sub_aminer_author2paper
INNER JOIN sub_aminer_paper ON sub_aminer_paper.pid = sub_aminer_author2paper.pid)
But it not works as required, it has inserted all the aid values at end of the table as
Please help!
Add a column named aid with null in sub_aminer_paper table. Then simply update it.
UPDATE A
SET A.aid = B.aid
FROM sub_aminer_paper A
INNER JOIN sub_aminer_author2paper B ON A.pid = B.pid
I think you are describing an UPDATE scenario here, not an INSERT, if I understand you correctly.
Update statements can include a from part with multiple tables joined like this:
UPDATE sub_aminer_paper
SET aid = sub_aminer_author2paper.aid
FROM sub_aminer_author2paper
INNER JOIN sub_aminer_paper ON sub_aminer_paper.pid = sub_aminer_author2paper.pid
This statement will update the aid column for existing rows in sub_aminer_paper with the aid value found in sub_aminer_author2paper for matching pid.

select all rows from a table and inserting only distinct values into a database at another location in C#

I have two databases at different location of similar structure, I want to select all the rows from one database table and insert only distinct values into another database table using C#.
You have to select into sql statement
SELECT * INTO TableName FROM [MS Access;PWD=password;DATABASE=C:\Docs\database2.mdb].TableName
Make sure the datatype is same for source and destination columns

query to dispaly from two tables with negation

I have two listboxes,listbox1 and listbox2, and two tables in a DB, table1 and table2. Both tables contain a column called 'Colour'. I want to display all 'Colours' from table1 in listbox1 which I‘m able to do. But in listbox2 I want to display 'Colours' from Table2 but it must not be present in the Table1 'Colours'. So how do I write a query for it?
This is what I have been trying and its not working:
SELECT Table2.Colour
FROM Table1 CROSS JOIN Table2
WHERE (Tabel1.Colour! = Table2.colour)
Error Message is — multi-part identifier Tabel2.Colour could not be found
This should work with a LEFT JOIN:
SELECT Table2.Colour
FROM Table2
LEFT JOIN Table1 ON Tabel1.Colour = Table2.colour
WHERE Table1.Colour IS NULL
You could also use NOT IN or NOT EXISTS, I just prefer the LEFT JOIN / IS NULL syntax.
For example:
SELECT Colour
FROM Table2
WHERE Colour NOT IN (SELECT Colour FROM Table1)
If your version of RDBMS implements EXCEPT clause,
you can do the following:
SELECT Colour FROM Table2
EXCEPT
SELECT Colour FROM Table1
I'm not saying that this is better than JOIN. Just another option.

UPDATE TableA.FK VALUES TableB.PK WHERE TableA.Column = TableB.Column

I'm trying to merge tables from different databases, ServerDB and ClientDB and save to ClientDB where the client's tables hold the master's tables records plus any records the user might add. On the other hand server tables could also be updated (new records inserted).
The database has relationships. The column in TableA I want to INSERT INTO values is a ForeignKey linking to TableB's PrimaryKey which is an auto-incremental column.
I'm saving all records from both databases in a merged dataset, and due to database design restrictions, I need to clear both tables on the client before inserting the merged tables from the dataset.
I first update TableB (the one with the PrimaryKey auto-increment column), but now the values of this column have nothing to do with the ForeignKey on TableA, so I update TableA and temporary inserting in the ForeignKey column the value of the first record of TableB's PK. Now I need to update TableA foreignKey column with the correct values from TableA PK column. Theres also a third column on each table that have the same values.
What the syntax of the sql statement should be? If I don't make much sense let me know and I'll post a better description.
It was a confussing question but i think you are talking about inserting values from table 1 to table 2 where table 1 value is equal to table 2 values the sql query for this operation is
INSERT INTO emp (empno,ename)
SELECT t2.deptno,
t2.dname
FROM dept t2
LEFT JOIN emp t1
ON t2.deptno = t1.deptno
in this query table 1 (emp) will insert 2 values into columns(empno and ename) from
table 2 (dept) and join is on (deptno) which is present in both tables.
You can further ask if this was not helpful.
Thank you all for your help. After struggling for a while with the "OledbException Operation must use an updateable query" I found out a solution in a similar topic: SQL Update woes in MS Access - Operation must use an updateable query
Thats the query that did the trick:
UPDATE DISTINCTROW PlaylistsSongs
INNER JOIN PlaylistsNames ON PlaylistNames.PlaylistName = PlaylistsSongs.PlaylistName
SET PlaylistID = PlaylistNames.ID

Categories

Resources