Have two mysql tables such as
Table1
Id
Field1
Field2
Table2
Id
LinkId
Field3
Field4
The common fields to link the tables are Table1.Id and Table2.LinkId.
Also important tha Table2 can have multiple rows where LinkId are the same.
So what I have been trying to figure is a mysql query to Select all rows in Table1 that have a linked row or more in Table2 where Field3 contains a certain value. Is this easily possible?
Simply use JOIN
SELECT Table1.*
FROM Table1 A JOIN Table2 B ON A.Id = B.LinkID
WHERE B.Field3 IN ('Your inputs')
You can have multiple tables in the FROM clause:
SELECT *
FROM Table1, Table2
WHERE Table2.Field3 = 'certain value'
AND Table1.Id = Table2.LinkId
Related
I have 2 mysql tables
table1 fields - id, field1, field2, field3, field4, field5
table2 fields - id, field3
What i need is result of this query
SELECT
t1.id,
t1.field1,
t1.field2,
t2.field3,
t1.field4,
t1.field5
FROM table1 as t1
INNER JOIN table2 as t2 ON t1.id = t2.id
WHERE 1
The problem I'm having is that I have more similar tables and in some cases table1 fields may not be 6 fields, but 50 fields.
That is why I need to make query look like
SELECT t1.*, t2.field3 as field3
FROM table1 as t1
INNER JOIN table2 as t2 ON t1.id = t2.id
WHERE 1
but in this query t1.field3 is selecting and showing in query result.
Can someone give an idea how to make my first query look like second query and return the result of the first query.
It is not possible with plain SQL, you can achieve with some programming using stored procedures, generating dynamic queries by first reading the column names from the table and then generating the query
I have two tables, in which table 1 contains 4 columns while table 2 contains 8 columns. I have two columns in table1 that I want to compare them with two columns in table2.
Table 1 have column1 and column2 (that needs to be compared)
Table 2 have column6 and column7 (that needs to be compared)
I need to compare the combination of the two columns. I tried to do the below query however it doesn't work
Select * from table1
where column1, column2 NOT IN (Select column6, column7 from table2)
How can I compare the two columns in the the two tables?
Except shows the difference between two tables (the Oracle guys use minus instead of except and the syntax and use is the same). It is used to compare the differences between two tables. For example, let's see the differences between the two tables
SELECT * FROM
table1
EXCEPT
SELECT * FROM
table2
Try a minus statement. This will give you any results from the first select statement of table1 that aren't in the second select statement on table2.
select column1, column2 from table1
minus
select column6, column7 from table2
NOT EXISTS is a "null safe" version of NOT IN.
If you mean the combination column1 AND column2 not in same row in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6
and table1.column2 = table2.column7)
Or if you mean just column1 and column2 values can't even be in different rows in table2:
select *
from table1
where NOT EXISTS (select 1 from table2
where table1.column1 = table2.column6)
and NOT EXISTS (select 1 from table2
where table1.column2 = table2.column7)
The query with the least comparisions I can think of is
Select t1.*
from table1 t1
left join table2 t2 on t1.column1 in (t2.column6, t2.column7)
or t1.column2 in (t2.column6, t2.column7)
where t2.column6 is null
SELECT * FROM table1 t1
RIGHT JOIN table2 t2
WHERE
t1.c1 = t2.c6 AND
t1.c2 = t2.c7
Please try this query:
Select
case when (table1.column1 = table2.column6)
then 1 else 0
end column1_6 check,
case when (table1.column2 = table2.column7)
then 1 else 0
end
from
table1
inner join
table2 on table1.ID = Table2.ID
select * from table1 where column1 not in(select column 6 from table2) or column2 not in(select column7 from table2)
This will give you rows from table1 where there are differences between col1 and col6 or col2 and col7
Hope this helps
I have 2 MySQL tables: table1 and table2
The field "table1.name" has records like "category.1298" where the number after the dot comes from the field ID in table2.
I would like to join table2.ID with table1.name to identify whether table2.ID is equal to the number (after the dot) in table1.name
The question is how to eliminate the portion "category." in table1.name
substring_index is what you're looking for:
SELECT *
FROM table1
JOIN table2 ON SUBSTRING_INDEX(table1.id, '.', -1) = table2.id
Use substring_index
Select substring_index(table1.name,'.',-1) returns 1298
select *
from table1, table2
where substring_index(table1.name,'.',-1) = table2.id
I'm running a query via PDO on a pair of joined tables like so:
SELECT table1.id, table2.id, table1.foo, table1.bar
FROM table1 INNER JOIN table2 ON table1.bar = table2.id;
Both tables have an id column so when I run fetchAll() the associative array only contains one id field. This is because the first is overwritten by the second.
Is there a way to obtain both id fields? Perhaps by having the table name included in the array keys...
use aliases
SELECT table1.id as t1id, table2.id as t2id
--etc.
try this
SELECT table1.id AS idtable1, table2.id AS idtable2, table1.foo, table1.bar
FROM table1 INNER JOIN table2 ON table1.bar = table2.id;
I have two table in database.
Table1 -> Name
Table2 -> Name
What will be query to get all the "Name" from Table1 and Table2 into single Column.
This query returns the value from the Name column from Table1 and the Name column from Table2, concatenated together into a single resultset.
SELECT t1.Name FROM Table1 t1
UNION ALL
SELECT t2.Name FROM Table2 t2
(This was my understanding of what you were looking for.)
If you want just a "distinct" list of Name values (exclude duplicate occurrences of the same value), then remove the ALL keyword.
If I correctly understood
http://dev.mysql.com/doc/refman/5.0/en/union.html
Select name from table1
union
Select name from table2
You can select data from two tables like this.
SELECT CONCAT(table1.name,table2.name) as Name FROM table1,table2;
and in case table1.name is A and table2.name is b you get
Name = AB
SELECT Name FROM Table1 NATURAL LEFT JOIN Table2 AS t2.
This will give you a list of only non-duplicate names from Table1 and Table2.