mysql query to display data using joins? - php

I have two tables I want to display datas which are not in table 2 but exist in table 1 and after listing the datas.I want to add those datas in table 2.
which join shall i use?please help me with the code

You need no join at all. You want data from table1 where no entry exists in table2. So use the EXISTS clause.
select something
from table1
where not exists
(
select *
from table2
where table2.somekey = table1.somekey
);
As to the insert:
insert into table2 (column names)
select something
from table1
where not exists
(
select *
from table2
where table2.somekey = table1.somekey
);

Related

Get mysql table name from query result

I want to display statistics about a mysql table using this Query:
select * from t1 where product = ABC
Union
select * from t2 where product = ABC
The above query produces 2 rows with product="ABC" in t1 and 3 rows with product="ABC" in t2. The query doesn't tell me which table it came from.
How can the query be changed so I can know the source table?
You can include a unique token in each query to let you know which table the data came from like this:
select *, 't1' as tablename from t1 where product = 'ABC'
Union
select *, 't2' as tablename from t2 where product = 'ABC'

select values with same cloumn a but different column b

I am using phpmyadmin database.
I have a table with columns 'admitno' and 'name' . I want to select values with same Admit No but different name and display those admitno along with all names against it.
I have tried code like
SELECT Admitno, Name
FROM table1 t1
WHERE EXISTS
(
SELECT 1 FROM table1 t2
WHERE t2.Admitno= t1.Admitno
AND t2.Name<> t1.Name
)
But not working in my case. Any suggestion please
You have to use self join and then filter data on the conditions that the two alias of table have same Admitno but different name
SELECT * from table1 a join table1 b where a.Admitno = b.Admitno and a.Name!=b.Name

PHP MySQL Display Joined and non- Joined Records

I am having a problem with not been able to display all records from table1.
I have 2 tables.
Table 1 and 2 and I want to display all the records from table 1 (Even if some records donly exists on table1 and no reference in table2)
This is what I am trying and I have 2 recording in Table1 but it's only displaying 1.
1 record is joined by the name_id on table1 and table2 and the other record only exists on table1 BUT I need to display both.
Here is the query:
$query = mysql_query("SELECT
table1.name_id,
table2.name_id,
FROM `table1`
LEFT JOIN `table2` ON table1.name_id=table2.name_id
");
How can I get it so it will display all the records from table1 (The one's that are join and the ones that are not too) ?
you can use "JOIN" to fetch data from both table as
$query = mysql_query("SELECT table1.name_id, table2.name_id FROM `table1` JOIN `table2` ON `table1`.`name_id`=`table2`.`name_id` ");

Selecting data from mysql table and related data from another to join them

Ive looked at other questions and answers but still dont understand which brings me here.
I have one data base two tables. lets say table1 and table2 from database.
I'm looking to grab all the information from table1 and only one column from table2 that coincides with the correct row in table1.
Example which I know is wrong:
SELECT table1.*, table2.time_stamp FROM table1, table2
WHERE table1.ticket_id=$var AND table1.user_id = table2.user_id
Basically select data from table1 then use a value from the selected table to grab the related data from table2 and join them to output them as one mysql_query. Im sure its simple and has been asked before.
edit:
I dont receive an error. SQL just returns noting. log form of this would be:
$sqlResults = mysql_query("SELECT table1.* FROM table1 WHERE table1.ticket_id=$var")
while($rowResult = mysql_fetch_array( $sqlResults )) {
$userID = $rowResult['user_id'];
$sqlResults2 = mysql_query("SELECT table2.time_stamp FROM table2
WHERE table2.user_id=$userID")
}
I want to combine that into one sql statement so i dont have to hit table2 for every row table1 has
Use a JOIN to bind the rows from table2 to those from table1:
SELECT t1.*, t2.time_stamp FROM table1 t1
JOIN table2 t2 ON t1.user_id = t2.user_id
WHERE t1.ticket_id=$var

PHP/SQL Statement repeating a query

If I have a list of ID's that I have selected from a statement
SELECT id FROM myTable WHERE name = 'TEST'
This would return just the ids (1001, 1002, 1003, etc...) Then I want to perform another SELECT statement to retrieve all the titles for all those ids.
SELECT title FROM myTable2 WHERE id = XXXX
the id in table2 is the foreign key of table2. id in myTable is the Primary Key. How can I go about retrieving all the titles from those ids. I was thinking about storing all the results of the first select statement in an array, and then using a while loop to iterate through the list and return each result into another array, but my fear is that when the database gets big if it has to return 1000 rows that could be some bad overhead. So in PHP or SQL what is the best way to perform this?
You can use a subquery:
SELECT title
FROM myTable2
WHERE id IN (
SELECT id
FROM myTable
WHERE name = 'TEST'
)
Another way to do it would to be use a JOIN, to avoid the sub-query:
SELECT title
FROM myTable2
LEFT JOIN myTable
ON myTable.id = myTable2.id
WHERE myTable.name = 'TEST'
You should just be able to select them at the same time.
SELECT a.id, b.title
FROM myTable a, myTable2 b
WHERE a.name = 'TEST' AND b.id = a.id;
to select both:
SELECT id, title FROM mytable WHERE name="TEST"
or to select the whole row
SELECT * FROM mytable WHERE name="TEST"
if its two tables you are selecting from:
SELECT id, title FROM mytable A JOIN mytable2 B USING (id)

Categories