I have 2 tables on my database. Table tableA looks like this:
taid | tanum | tarelation
---------------------------
30 | 22 | 101
31 | 88 | 101
And table tableB looks like this:
tbid | tbnum | tbrelation
---------------------------
1 | 10 | 101
2 | 20 | 101
I want to echo out all rows for tanum and tbnum - should be only 4 results echoed out, but instead my code is doubling each result echoed, so it is displaying 8 results total (should not do that). This is the unwanted result I am getting:
22
10
88
10
22
20
88
20
Why is it doing that? The result should not look like that, but like this:
22
88
10
20
Please help me fix this. This is my code:
<?php
$columns = [];
$stmt = $conn->prepare("
SELECT tableA.*, tableB.*
FROM tableA
INNER JOIN tableB
ON tableA.tarelation = tableB.tbrelation");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_object()) {
$columns[] = $row;
}
$stmt->close();
?>
<div>
<?php foreach($columns as $column): ?>
<div><?php echo $column->tanum; ?></div>
<div><?php echo $column->tbnum; ?></div>
<?php endforeach; ?>
</div>
Use UNION concept. For that, you can use UNION or UNION ALL keyword.
Note:
While using UNION, duplicate records will be removed, so if you want to keep all records use UNION ALL.
You don't need to create sub-queries for that.
SELECT taid AS ID, tanum AS NUM, tarelation AS RELATION FROM tableA
UNION ALL
SELECT tbid AS ID, tbnum AS NUM, tbrelation AS RELATION FROM tableB;
Your SQL query does not produce a list of just the tanum and tbnum values that you show as your output. If you want a list of just the tanum and tbnum values in all tables (as you show as your desired result), this will do it:
select tanum from TableA
union
select tbnum from TableB;
I am not sure if this is what you are looking for, but it seems like you just want to append both these tables:
select a.*
from
(select taid as ID,tanum as NUM,tarelation as RELATION from tableA
UNION
select tbid as ID,tbnum as NUM,tbrelation as RELATION from tableB)a
SQL query is fine.
I believe the following result looks like something expected by you:
But you are just printing values twice by yourself:
<div><?php echo $column->tanum; ?></div>
<div><?php echo $column->tbnum; ?></div>
Row 1, "tanum": 22
Row 1, "tbnum": 10
Row 2, "tanum": 88
Row 2, "tbnum": 10
Row 3, "tanum": 22
Row 3, "tbnum": 20
Row 4, "tanum": 88
Row 5, "tbnum": 20
It's the SQL
The inner join doesn't ensure that only 1 rows in both table will be connected
Related
I need help with the below code -
I have 2 tables - A and B
A | B
====================
1 | 1
2 | 2
3 | 3
All i need is the row(1) from table A should be allowed to merge with row(2) and row(3) in tableB, likewise tableA row(2) should be allowed to merge with row(1) and row(3) of tableB and tableA row(3) should be allowed to merge with row(1) and row(2) of tableB.
[this is just an example]
======================
What I have tried:
I tried to use
$array = array('1=>1','2=>2','3=>');
$query ="select A from table where B='".$T."' union all select B from table where A='".$T."'";
$array = array('1=>1','2=>2','3=>');
$query ="select A from table where B='".$T."' union all select B from table where A='".$T."'";
$stid = oci_parse($conn,$query);
$row = oci_execute($stid);
$row = oci_fetch_row($stid);
$oci_free_statement($stid);
I am not able to sure if we need to use slice/intersect/subset.
I have been trying to sort this using OCI_bind_by_array as well but no go, i have spent 3days but unable to find a solution.
I've a table syntax like this,
- table1
id foreign_table_id
-----------------------
1 2
2 2
3 2
4 1
5 1
6 1
The other table is,
- table2
id value
------------
1 20
2 10
I want to get a summation of table1.foreign_table_id where the data will retrieve from table2.
Like this example should produce the result 90
Please provide any solution.
Use below query.
Select sum(table2.value) as total FROM table1 LEFT JOIN table2 ON table1.foreign_table_id = table2.id
It will give u below result.
Output::
total
-----
90
SELECT SUM(table2.value) AS summation FROM table1 LEFT JOIN table2
ON table2.id = table1.foreign_table_id
Hi I'm trying to get the num rows of every data that equal on the value produce by foreach loop here's my samp code
$query = $this->db->query("SELECT * FROM tblsamp");
foreach ($query->result() as $row){
$subquery = $this->db->query("SELECT * FROM other_table WHERE foo like '%$row->some_col%'");
echo $subquery->num_rows();
// Ill get the num rows here equal on the value of $row->som_col
}
Here's the sample output so you can visualize the data"
name | NoRows
name1 | 6
name2 | 6
name3 | 6
name4 | 6
the problem is if name1 detect '6' rows the name2,name3,name4. and so on will also output '6' rows which is that the number of rows on each name is different.
Why im getting the same result of name1?
Hoping for your answer guys! thanks!
Why don't you use group and count request ?
select t.name, count(*)
from tblsamp t
left join other_table on foo like concat('%',t.some_col,'%')
group by t.name
In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .
I have a table that I'm querying for value 43 in the second field and I return the value of the third field
SELECT t1_field3 FROM table1 WHERE t1_field2=43
this returns 19, 39,73
t1_id t1_field2 t1_field3
----- --------- ---------
1 43 19////
2 43 39////
3 43 73////
4 73 43
5 13 40
Then I separately query a second table for additional information
SELECT * FROM table2 WHERE t2_id=t1_field3
t2_id t2_field2 t2_field3
----- --------- ---------
19 value19.2 value19.3
39 value39.2 value39.3
73 value73.2 value73.3
Is there a way I could combine both table1 and table2 in the same query?
You're describing a JOIN. In this case you don't need to explicitly use the JOIN keyword though, you can just do this:
SELECT table1.t1_field3, table2.* FROM table1, table2 WHERE table1.t1_field2=43 AND table2.t2_id = table1.t1_field3
It would probably be helpful to learn about the different types of joins at some point; Coding Horror has a good post about it
select * from table2
where t2_id in (select t1_field3 from table1 where t1_field2=43 )
There's a way to express the join directly as well like so:
select table1.t1_field3, table2.*
from table1
join table2 on table1.t1_field3 = table2.t2_id
where table1.t1_field2 = 43;