Mysql Select from table1 where row1 equals row1 in table2 in php? - php

Ok, so I can't seem to get this to work, but here is what I wan't to do. I have 2 tables one like this
Table1
----------
Row1
And this
Table2
----------
Row2
I want to get the data from Table1 where Row2 equals "thisvalue", in php. Any idea how I should go about doing this? Thanks.

You join the tables together
SELECT table1.* FROM table1 t1 JOIN table2 t2 ON t1.row1 = t2.row2
If you want only a row with a specific rw2-value, you can specify that in the WHERE clause
SELECT table1.* FROM table1 t1 JOIN table2 t2 ON t1.row1 = t2.row2 where t2.row='your value'

Related

How to copy data from one table to another based on ids present in third table in MySQL?

I have three tables like
Table1
It's a blank table that have same columns as Table2.
Table2
It contains the actual data that needs to be copied in Table1.
id cola colb colc cold
1 hd dj dj dh
2 hf uy ug se
...
Table3
Before copying data from Table2 to Table1, first I need to verify that if the id is present in Table3 or not. In other words I just want to copy the rows from Table2 to Table1 whose id exist in Table3.
id col1 col2
1 xy zz
2 ys sh
One more thing Table2 & Table3 contains half millions of rows, so query must be feasible.
some one make this as a duplicate question
however the answer is
insert into Table1
select * from Table2 where Table1.id=Table2.id
Option 1 using subquery:
insert into Table1
select * from Table2 where id in (select id from Table3)
Option 2 using INNER JOIN:
insert into Table1
select * from Table2 INNER JOIN Table3 USING(id);
insert into Table1
select * from Table2 where id in (select id from Table3)
Above query will work if Number of columns in table1 and table2 are same
Else
insert into Table1 (column1,column2,.....)
select column1,column2... from Table2 where id in (select id from Table3)
Use join. It is faster.
insert into table1 (id, cola, colb, colc, cold)
select t2.id,t2.cola,t2.colb,t2.colc,t2.cold
from table2 t2
left join
table3 t3
on t2.id=t3.id
where t3.id is null
You can use EXISTS
Query
insert into Table1(id, cola, colb, colc, cold)
select id, cola, colb, colc, cold
from Table2 t
where exists(
select * from Table3
where id = t.id
);
OR with JOIN.
insert into id, cola, colb, colc, cold
select t1.id, t1.cola, t1.colb, t1.colc, t1.cold
from Table2 t1
join Table3 t2
on t1.id = t2.id;

How to obtain search query using join command in mysql

I have two table
Table 1:
Ensemble_ID
Target
Gene_length
miRNA
miRNA_length
mfe
pvalue
position
prediction
no_of_seeds
And in table2
Ensemble_ID
local_ID
I want to display the result in the following order,
Ensemble_ID, local_ID, Ensemble_ID, Target ,Gene_length, miRNA, miRNA_length, mfe, pvalue, position, prediction, no_of_seeds
But i could not join it .. Can some one help ??
This is what you are asking:
SELECT t1.Ensemble_ID AS Ensemble_ID1, t2.local_ID, t2.Ensemble_ID AS Ensemble_ID2,
t1.Target, t1.Gene_length, t1.miRNA,
t1.miRNA_length, t1.mfe, t1.pvalue, t1.position, t1.prediction, t1.no_of_seeds
FROM table1 t1, table2 t2
But it think you want something like this:
SELECT t2.local_ID, t2.Ensemble_ID, t1.Target, t1.Gene_length, t1.miRNA,
t1.miRNA_length, t1.mfe, t1.pvalue, t1.position, t1.prediction, t1.no_of_seeds
FROM table1 t1
INNER JOIN table2 t2 ON (t2.Ensemble_ID = t1.Ensemble_ID)
If you want to join table1 and table2 on Ensemble_ID there is no need to output it twice

Join tables on missing matched rows from the second table

I have two database tables - table1 and table2. For some records in table1 i have several rows connected in table2. For most of them i have 3 rows connected, but for some of them i have an extra row with a column value like table2.field='correct'. How can i join table1 and table2 if i want the result to return only the rows from table1 where there is no row in table2 with column value like table2.field='correct' connected to them ? Counting the number of rows from the second table( if num of connected rows < 4 or something like that is not an option).
I tried something like :
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL
but it did not work ofc. because i always have rows with value in field column. For each row in t1 that is connected to t2 i have record rows in t2 where t2.filed='name' and t2.field='type'. I need the rows from t1 that do not have a row connected to them in t2 where t2.field='correct'.
Use NOT IN
SELECT * from t1 WHERE t1.id NOT IN(SELECT id FROM t2 WHERE t2.field = 'correct')
$sql = "SELECT t1.* FROM table1 t1 CROSS JOIN table2 t2 on t1.id=t2.id_t1 WHERE t2.field IS NULL ";
SELECT t1.* FROM table1 t1 WHERE NOT EXISTS (SELECT t2.* FROM table2 t2 WHERE t1.id=t2.id_t1 AND t2.field = 'correct')

check if name from table 1 exists in table 2

Hello i searched on google but i could not find it(maybe wrong search terms) But i'm asking if there is a way to check if name1 from table 1 exists in table 2
So like
select name from table 1.
search in table 2 for the name from table 1
is this possible? if yes how?
~Kev (bad english = sorry)
Select name from table1 Inner Join table2 on table1.name = table2.name;
Depending on your structure this will give you all the names which exists both in table1 and table2 since the joining is done itself on the name
Perhaps something like this (untested)
SELECT name1 FROM tableA WHERE name1= (SELECT name2 FROM table2 WHERE .... )
You are asking about joins between 2 tables. To query all entries from table 1 that exists in table 2 you need next SQL:
SELECT * FROM table1 t1 INNER JOIN table2 t2 ON t1.name = t2.name
$s2="select * from trackyesttrackyest";
$q2=mysql_query($s2) or die($s2);
$row=mysql_fetch_array($q2);
$s="select * from <secondtablename> where rsname='".$row['rsname']."'";
$q=mysql_query($s) or die($s);
$row2=mysql_fetch_array($q);
please refer this link
click here
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Its simple buddy.........
try out this...
SELECT * FROM table1 WHERE table1.name in (SELECT table2.name FROM table2)

Bulding a query of three tables in mysql

I have to three table I need to gather data from in a search process:
Commissions Table - Table1: [affiliate_id]
Affiliates Table - Table2: [id][user_id]
Profiles Table - Table3: [ID][NickName]
The search input I'll have is someone searched for a username. I need to return the data from table 1 where the affiliate_id matches the user_id of Table2, that is like the nickname that will be searched for.
I hope that makes sense :)
Try this:
"select table1.* from table1
inner join table2 on table2.user_id = table1.affiliate_id
inner join table3 on table3.id = table2.user_id
where table3.nickname like '%".mysql_real_escape_string($searchtext)."%'"
SELECT t1.*, t3.nickname FROM Table1 t1
JOIN Table2 t2 ON t2.id=t1.affiliate_id
JOIN Table3 t3 ON t2.user_id=t3.user_id
WHERE t2.user_id=?;

Categories