Count the number of rows with the same id [PHP] - php

So I want to count and echo the number of rows from table2, which have the same id row as table1.
I understand the function mysqli_num_rows funtion, but how can I make it count the rows from table2 that have the matching id?
Total newbie, and still searching. Thought this might help me get in the right direction. Thanks for any help !

mysqli_num_rows return the number of rows from a result.
You should write an other query to return the number of rows in table 2 with the same id
SELECT COUNT(*)
FROM table2
WHERE table2.id = <id>

CREATE TABLE table1 (id INT);
CREATE TABLE table2 (id INT);
INSERT INTO table1 VALUES (1), (2), (3), (4);
INSERT INTO table2 VALUES (2), (4);
SELECT COUNT(*)
FROM table1
JOIN table2
ON table1.id = table2.id;
Try it here: http://sqlfiddle.com/#!9/0d289/1

To only count
select count(*)
from table1, table2
where table1.id = table2.id
To know what id and how much
select table2.id, count(*)
from table1, table2
where table1.id = table2.id
group by table2.id
The second case on sqlfiddle.com

Try this:
SELECT `table2`.`id`, COUNT(`table2`.`id`) AS `num_rows`
FROM `table2`
LEFT JOIN `table1` ON `table2`.`id` = `table1`.`id`
WHERE `table2`.`id` IS NOT NULL
GROUP BY `table2`.`id`
ORDER BY `table2`.`id` ASC

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;

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')

PHP SQL Query Fields from Another Table Join

I am trying to get a query using fields from 2 tables.
I need to query Table1 but only Table2 has the variable venue_location that I need to query.
Basically I need to count all records on Table1 where Table1.venue_location = $MyVariable.
Here is what I've put together but I believe I need to use Joins for this?
Table1
- venue_id
Table2
- venue_id,
- venue_location
SELECT * FROM Table1 WHERE table1.venue_id = table2.venue_id and table2.location = '$MyVariable'
How can I do a query for this?
Use the power of join table
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
You can get back the count of rows with mysqli_num_rows() in PHP, or change le select by SELECT COUNT(*) AS nbRow FROM ... and check of value in nbRow column
You can join two tables on venue_id and then group it by venue_id where location is your $MyVariable.
Your final query will look like:
SELECT count(table2.venue_id)
FROM Table1
JOIN Table2 ON table1.venue_id = table2.venue_id
WHERE table2.location = '$MyVariable'
GROUP BY table2.venue_id
try this
SELECT Table1.venue_id, Table2.venue_location FROM Table1 INNER JOIN Table2
ON Table1.venue_id='$MyVariable';

Select unmatched records from two tables of MYSQL

I have two tables Table1 and Table2 with some records
id is the common column in both tables and primarykey is set to this column in table1
There are many records in table1 and some of these records (not all) are updated into table2.
Now I want retrieve from table1 the records not updated into the table2.
For example in table1 there are records 1,2,3,4,5,6,7,8,9
And in table2 there are 3,4,7,9
Now How can I retrieve these records form table1 1,2,5,6 those not updated into table2
I wrote this query :
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME
FROM [Table1] INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id;
But the expected result not coming. This query lists all the records repeating each one record manytimes
Can any body give me solution to get the expected result.
select *
from table1
where table1.slip_no NOT IN (select id from table2)
Assuming name of common column is id
Or you can modify your query as
SELECT distinct (Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME)
FROM [Table1]
INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id
A good reference on SQL joins
SELECT t1.*
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 USING(id)
WHERE
t2.id IS NULL;
You can use the NOT IN operator on a subquery for table2.
Alternatively, use MINUS with two regular queries listing the ids in each table:
SELECT id FROM table1
MINUS
SELECT id FROM table2;
Try this
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME FROM [Table1]
WHERE
NOT EXISTS (SELECT * from Table2 WHERE Table1.SLIPNO !=Table2.id );
You can use the following query
SELECT id FROM database1.table WHERE id NOT IN(SELECT id FROM database2.table)
SELECT child_table.id FROM child_table LEFT JOIN parent_table ON child_table.parent_id = parent_table.id WHERE parent_table.id IS NULL
This left join query returns all the records of the child_table when there is no match in the parent_table. When there is no match, all parent_table fields will be NULL.
inner join will not help. To get unmatched records I tried this:
SELECT
A.ID,A.DATE,A.NAME
FROM TABLE1 A
WHERE CONCAT(A.ID , A.DATE ,A.NAME)
NOT IN
(SELECT CONCAT(B.ID , B.DATE ,B.NAME) as X
from TABLE2 B) ;

How do I compare two tables data?

Hi I do have an old table and new table with same index/data. TABLE1 and TABLE2
but TABLE1 has got more data than TABLE2. this was maintained by some one and I dont know how this happened. so my question is how do I compare these two table and find which data is TABLE2 missing?? there is almost 200000 datas there so manually doing is not possible...
in PHP:
http://us.php.net/manual/en/function.array-diff.php
in SQL:
SELECT * FROM TABLE1 WHERE id {NOT} IN ( SELECT id FROM TABLE2 )
depending on criteria of comparison
Solution without nested query:
select TABLE1.id from TABLE1 left join TABLE2 on TABLE1.id = TABLE2.id where TABLE2.id is null
Did you mean something like this:
SELECT * FROM TABLE1 t1 WHERE NOT EXISTS(SELECT * FROM TABLE2 WHERE t1.id == t2.id)
By same index I am hoping you mean they share a primary key?
SELECT * FROM TABLE1 WHERE username NOT IN (SELECT username FROM TABLE2)

Categories