PB / SQLite3 JOIN COMPARE TABLES - php

I have Two tables :
T1
"IdT1;i1T1,n2T1
1;123;n2T1
2;234;n3T1
3;345;n4T1
4;678;n1T1
5;123;n2T1
6;234;n3T1
T2
"idT2;n1T2;n2T2;i1T2
1;1n1T2;2n1T2;123
2;1n2T2;2n1T2;234
3;1n3T2;2n1T2;345
4;1n4T2;2n1T2;456
5;1n5T2;2n1T2;567
6;1n6T2;2n1T2;678
In SQLITE3
$req = $db -> prepare("SELECT T2.n1T2, T2.n2T2,
//COUNT(*)
FROM T1
INNER JOIN T2 ON T1.idT2 = T2.idT2
");
if ($res = $req->execute()) {
$arr = $res->fetchArray();
var_dump($arr);
}
I search for n1T2 and n2T2 names, the count of same values between i1T1 and i1T2
example:
1n1T2;2n1T2;2
1n2T2;2n2T2;2
1n3T2;2n1T2;1
...
I var_dump for display, in wait best method

It looks like you want a join and aggregation:
select t2.n1t2, t2.n2t2, count(*) cnt
from t1
inner join t2 on t2.i1t2 = t1.i1t1
group by t2.n1t2, t2.n2t2

Related

How can I access data on columns from a specific table from a JOIN query statement

Suppose this is the SQL
SELECT * FROM core AS t1
LEFT JOIN supply AS t2
ON t1.sid = t2.id
LEFT JOIN dingus AS t3
ON t1.did = t3.id
LEFT JOIN covid AS t4
ON t1.cid = t4.id
LEFT JOIN mom AS t4
ON t1.mid = t4.id
Here's some code
Normally getting the data from the database would be something like this
$sql = "SELECT * FROM core AS t1
LEFT JOIN supply AS t2
ON t1.sid = t2.id
LEFT JOIN dingus AS t3
ON t1.did = t3.id
LEFT JOIN covid AS t4
ON t1.cid = t4.id
LEFT JOIN mom AS t4
ON t1.mid = t4.id;";
$sts = self::$db->prepare($sql);
$sts->execute();
$rows = $sts->fetchAll(PDO::FETCH_NUM); // Or what ever it is,
// it could be PDO::FETCH_ACCOS, or others, but I couldn't find the right one.
$sts->closeCursor();
return $rows
Then you loop though all the rows with
foreach($rows as $row)
{
echo $row[0];
/// ... and so on
}
Are there any ways to get data from column of a specific joined table?
Something like this...
foreach($rows as $row)
{
// This represents data of the first column of the table dingus
echo $row->t2[0]; // Something like this..
}

Can I get data from a table based on multiple other tables?

$query = "SELECT * FROM table3 WHERE name_id = '(SELECT name_id FROM table2
WHERE salary < 1000 && name = '(SELECT name FROM table1
WHERE savings > 1000)')'";
Basically I want to get the data from the table1 based on the savings and use it to get the data from table 2 and use that data to get all the information from table 3. But this wont work. Is my code right or am I doing something wrong?
I also cannot create new tables, I simply want to display the data from table 3.
Use join
SELECT * FROM table3 t3 join table2 t2
on t3.name_id=t2.name_id
join table1 t1
on t3.name=t1.name
where salary < 1000 and savings > 1000
$query="SELECT * FROM table3 LEFT JOIN table2 ON table3.name_id=table2.name_id
LEFT JOIN table1 ON table3.name=table1.name
WHERE table2.salary < 1000 AND table1.savings > 1000 "
Another syntax of join is
SELECT * FROM table1 t1,table2 t2 ,table3 t3
where t1.name = t3.name and
t2.name_id = t3.name_id and
t1.savings > 1000 and t2.salary < 1000;
$query = "SELECT t3.* FROM table3 t3
INNER JOIN table2 t2 ON t2.name_id = t3.name_id AND t2.salary < 1000
INNER JOIN table1 t1 ON t1.name = t2.name AND t1.savings > 1000";

sql, join 2 Tables

I have 2 tables
matchdata:
id_team1, id_team2, name_team1, name_team2, group_order_id
teams:
team_id, team_name
I try this:
select * from matchdata join teams on matchdata.id_team1 = teams.team_id;
But I need
matchdata.id_team1 = teams.team_id
AND
matchdata.id_team2 = teams.team_id
after that I want select WHERE group_order_id = $bla
How can I do that?
You can use two JOIN on a single table.
SELECT * FROM matchdata
JOIN teams t1 ON matchdata.id_team1 = t1.team_id
JOIN teams t2 ON matchdata.id_team1 = t2.team_id
WHERE group_order_id = $bla;
You can use IN for this:
SELECT *
FROM matchdata md
JOIN teams t ON t.team_id IN (md.id_team1, md.id_team2)
WHERE md.group_order_id = $bla

Search Function is Hang with 2 year data and join

here us the query
SELECT *
FROM Table1
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
AND order_id IN
(SELECT DISTINCT t1.order_id
FROM Table2 t1
INNER JOIN table3 t2 ON t1.prod_id = t2.prod_id
WHERE t2.prod_sku LIKE '%D-600%'
AND t1.create_dttm > '2013-02-15 08:28:41')
You are using a sub-query in WHERE clause, that could be the main reason behind slow execution of your query. Try using JOINS instead of sub query.
SELECT t1.*
FROM Table1 t1
INNER JOIN Table2 t2 ON T1.order_id = T2.order_id
AND t2.create_dttm > '2013-02-15 08:28:41'
INNER JOIN table3 t3 ON t2.prod_id = t3.prod_id
AND t3.prod_sku LIKE '%D-600%'
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
And also check for indexes on your tables.

I need a good mysql query to receive information from 3 tables

I have this function:
function view_user_anunt($user) {
$query="SELECT * FROM `anunturi`
FULL OUTER JOIN tranzactie
ON anunturi.tranzactie = tranzactie.id_tranzactie WHERE `anunturi.user`=:code";
$stmt = $this->dbh->prepare($query);
$stmt->bindParam(':code', $user, PDO::PARAM_INT);
$stmt->execute();
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $result)
{
$view[]="
<tr>
<td>".$result['id_anunt']."</td>
<td>".$result['den_tranzactie']."</td>
<td>".$result['den_proprietate']."</td>
<td><a href='#' id='vizualizare'>Select</a></td>
<td><a href='#' id='modificare'>Select</a></td>
</tr>";
}
return $view;
}
and 3 tables:
anunturi
id_anunt (int) auto increment
tranzactie(int)
tip (int)
user(int)
tranzactie
id_tranzactie (int) auto increment
den_tranzactie varchar
tip
id_proprietate (int) auto increment
den_proprietate varchar
I need a good query or an ideea to get transaction name (den_tranzactie) and proprietate name den_proprietate for each row from anunturi where anunturi.user = $user.
Thanks in advance...
Try this sql query
$query="SELECT t1.*, t2.*, t3.*
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.foreightkey_id
LEFT JOIN table3 t3 ON t1.id = t3.foreightkey_id
WHERE t1.user=:code
";
You can use INNER JOIN to receive only the rows witch have data in all tables. for example
$query="SELECT t1.*, t2.*, t3.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.foreightkey_id
INNER JOIN table3 t3 ON t1.id = t3.foreightkey_id
WHERE t1.user=:code
";
or if you want get all data from table1 and table2 which are connected and from table 3 only that which are connected
$query="SELECT t1.*, t2.*, t3.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.foreightkey_id
LEFT JOIN table3 t3 ON t1.id = t3.foreightkey_id
WHERE t1.user=:code
";
Something like this?
'
SELECT t.den_tranzactie, tip.den_proprietate
FROM
anunturi a
JOIN tranzactie t ON a.tranzactie = t.id_tranzactie
JOIN tip ON a.tip = tip.id_proprietate
WHERE a.user =:code

Categories