How to get one colum from the join of 2 tables SQL - php

I have 2 tables like this:
Table1
id | name
------------------
1 | David
2 | Lucas
3 | Antonio
Table2
id | name
------------------
1 | Sergio
2 | Sergio
3 | Lucas
I want to select data to group duplicate records and return this:
name
------------------
David
Lucas
Antonio
Sergio
So I tried with this query
SELECT name FROM Table1 JOIN Table2 GROUP BY name
But nothing is returned.

You need UNION instead of JOIN :
SELECT name
FROM table1
UNION
SELECT name
FROM table2;
JOIN is used for matching rows and produce subsequent columns from joined tables while union will combine all rows from two or more tables.

use union
select name fron table1
union
select name from table2

Related

Using UNION in prepared statements in PHP/MySQL [duplicate]

I have two tables (Table A and Table B).
These have different number of columns - Say Table A has more columns.
How can I union these two table and get null for the columns that Table B does not have?
Add extra columns as null for the table having less columns like
Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2
I came here and followed above answer. But mismatch in the Order of data type caused an error. The below description from another answer will come handy.
Are the results above the same as the sequence of columns in your table? because oracle is strict in column orders. this example below produces an error:
create table test1_1790 (
col_a varchar2(30),
col_b number,
col_c date);
create table test2_1790 (
col_a varchar2(30),
col_c date,
col_b number);
select * from test1_1790
union all
select * from test2_1790;
ORA-01790: expression must have same datatype as corresponding expression
As you see the root cause of the error is in the mismatching column ordering that is implied by the use of * as column list specifier. This type of errors can be easily avoided by entering the column list explicitly:
select col_a, col_b, col_c from test1_1790
union all
select col_a, col_b, col_c from test2_1790;
A more frequent scenario for this error is when you inadvertently swap (or shift) two or more columns in the SELECT list:
select col_a, col_b, col_c from test1_1790
union all
select col_a, col_c, col_b from test2_1790;
OR if the above does not solve your problem, how about creating an ALIAS in the columns like this: (the query is not the same as yours but the point here is how to add alias in the column.)
SELECT id_table_a,
desc_table_a,
table_b.id_user as iUserID,
table_c.field as iField
UNION
SELECT id_table_a,
desc_table_a,
table_c.id_user as iUserID,
table_c.field as iField
Normally you need to have the same number of columns when you're using set based operators so Kangkan's answer is correct.
SAS SQL has specific operator to handle that scenario:
SAS(R) 9.3 SQL Procedure User's Guide
CORRESPONDING (CORR) Keyword
The CORRESPONDING keyword is used only when a set operator is specified. CORR causes PROC SQL to match the columns in table expressions by name and not by ordinal position. Columns that do not match by name are excluded from the result table, except for the OUTER UNION operator.
SELECT * FROM tabA
OUTER UNION CORR
SELECT * FROM tabB;
For:
+---+---+
| a | b |
+---+---+
| 1 | X |
| 2 | Y |
+---+---+
OUTER UNION CORR
+---+---+
| b | d |
+---+---+
| U | 1 |
+---+---+
<=>
+----+----+---+
| a | b | d |
+----+----+---+
| 1 | X | |
| 2 | Y | |
| | U | 1 |
+----+----+---+
U-SQL supports similar concept:
OUTER UNION BY NAME ON (*)
OUTER
requires the BY NAME clause and the ON list. As opposed to the other set expressions, the output schema of the OUTER UNION includes both the matching columns and the non-matching columns from both sides. This creates a situation where each row coming from one of the sides has "missing columns" that are present only on the other side. For such columns, default values are supplied for the "missing cells". The default values are null for nullable types and the .Net default value for the non-nullable types (e.g., 0 for int).
BY NAME
is required when used with OUTER. The clause indicates that the union is matching up values not based on position but by name of the columns. If the BY NAME clause is not specified, the matching is done positionally.
If the ON clause includes the “*” symbol (it may be specified as the last or the only member of the list), then extra name matches beyond those in the ON clause are allowed, and the result’s columns include all matching columns in the order they are present in the left argument.
And code:
#result =
SELECT * FROM #left
OUTER UNION BY NAME ON (*)
SELECT * FROM #right;
EDIT:
The concept of outer union is supported by KQL:
kind:
inner - The result has the subset of columns that are common to all of the input tables.
outer - The result has all the columns that occur in any of the inputs. Cells that were not defined by an input row are set to null.
Example:
let t1 = datatable(col1:long, col2:string)
[1, "a",
2, "b",
3, "c"];
let t2 = datatable(col3:long)
[1,3];
t1 | union kind=outer t2;
Output:
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 1 | a | |
| 2 | b | |
| 3 | c | |
| | | 1 |
| | | 3 |
+------+------+------+
demo
if only 1 row, you can use join
Select t1.Col1, t1.Col2, t1.Col3, t2.Col4, t2.Col5 from Table1 t1 join Table2 t2;

Join two tables based on table_1 column value to table_2 column name that matches?

Is it possible to join 2 tables together on the basis of table 1 column value equaling table_2 column name?
table_1 table_2
----- -------------
| A | | 1 | 2 | 3 |
|---| |-----------|
| 1 | | Q | W | E |
----- -------------
Desired result is:
Select A from table_1 join table_2 WHERE table_1 A = table_2 column name.
Result:
1Q
I am using PHP, sorry for the poor psudo SQL, I honestly have no idea how to word it. Thank you for any help you can offer!
You want SQL where you use a value from one column as the name of another column. You Can't Do That™. In pure SQL queries the names of tables and columns must be literals (using the lingo of php style languages.
You probably can get the result you need by reading your tables into RAM with your php program, then programming your table-to-table correlation.
SELECT CONCAT(table_1.A,"",table_2.1) as RESULT
FROM table_1
LEFT JOIN table_2 ON table_1.A = table_2.1

Join the values of 2 tables

I have 2 tables and I can't merge the values of these tables into table2.
Table1:
id | studnum | fname | lname | mname
1 | 1001 | Mark | Lei | Ramos
Table2:
id | studnum | remarks
Is it possible that when I input the values into Table2, the studnum from Table1 will input into Table2 also?
I tried this but it doesn't worked.
$sql = "SELECT table1.studnum FROM table1, table2
WHERE table1.studnum = table2.studnum";
So that the Table2 will be like this output
id | studnum | remarks
1 | 1001 | good
What you're looking for is using a JOIN Statement.
If the shared key is between them is studnum, you can do
SELECT * FROM table1
JOIN table2
ON table1.studnum = table2.studnum
Try a MySQL join function. Example:
SELECT *
FROM table1
JOIN table2
ON table1.studnum=table2.studnum;
More reading on joins: http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php
If those are exactly your tables you dont really need what you are requesting as it goes against normalization.
In the case you will only have one or none remarks for an entry in table 1 there is no need for table 2, the remarks should be in table 1.
In the case you need multiple remarks for a single entry in table 1, you dont need stdnum in table 2. If your problem in this scenario is to construct a query to get the remakrs based on stdnums you can easily achieve this with a joint.
The only field you should be duplicating from table to table for a record/object is the id. You can (and may be should) scrifice normalization in pro of efficiency but your example is not the case.

querying for rows in a table and exempting specific rows if they exist in another

I have two different tables with a similar column in both. And i need to query for all rows in table A but must exempt specific rows in table A if those rows exist in table B.
Example:
Table A
---------------------------------
item_id | item_name | price
---------------------------------
1 | apple | 100
---------------------------------
2 | banana | 150
---------------------------------
3 | ginger | 120
---------------------------------
4 | pear | 150
---------------------------------
5 | berry | 120
---------------------------------
Table B
---------------------------------
item_id | item_owner |
---------------------------------
1 | Ben |
---------------------------------
2 | Damian |
---------------------------------
3 | Greg |
---------------------------------
Based on the example above, I need to run a query to fetch all the rows in table A if item_id does not exist in table B.
The result of this query should fetch only 2 rows which are:
---------------------------------
4 | pear | 150
---------------------------------
5 | berry | 120
---------------------------------
Would ber glad to get help with this...Thank!
use LEFT JOIN
SELECT a.*
FROM tableA a
LEFT JOIN tableB b
ON a.item_id = b.item_id
WHERE b.item_id IS NULL
SQLFiddle Demo
For faster performance, you must define an INDEX on column item_id on both tables to prevent server to perform FULL TABLE SCAN.
To fully gain knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
SELECT *
FROM TableA
WHERE item_id NOT IN(SELECT item_id FROM tableb);
Demo.
Try this::
Select
A.*
from
A LEFT JOIN B on (A.item_id=B.item_id)
where B.item_id is null
Try this query...
SELECT A.*
FROM TableA A FULL OUTER JOIN TableB
ON A.Item_id = B.Item_ID
WHERE B.Item_ID IS NULL

Compare two tables and update second table

There are two tables table1 and table2
table1 has got two columns name and rank
table2 has got only one column name
names in table2 are almost listed in table1
I want compare two table and pull rank info from table1 and update/alter table2 with rank
table1
name | rank
-------------
john | 2
mathews| 5
keyn | 4
emly | 25
yancy | 8
stewart| 9
kim | 12
chris | 19
table2
name
-------
john
mathews
keyn
emly
yancy
stewart
I want update/insert rank details to table2 from table1
thats it and sorry for the confusion
Seem like you want to do something like this:
update table2,table1 set table2.rank=table1.rank where table2.name=table1.name
This will update the second table with ranks from the first table where the names are equal.
Then put auto increament field of table one in table2. and after this apply left join using these id and pull the info
refer this link
http://www.wellho.net/solutions/mysql-left-joins-to-link-three-or-more-tables.html

Categories