please can anyone help me, I am familiar with PHP but I'm not good in it.please help. I have two table and both have id, see sample,
Table1
----------------------------------
**ID** | **NAME** | **AGE** |
----------------------------------
001 | john | 21 |
----------------------------------
002 | erik | 18 |
----------------------------------
003 | ella | 19 |
----------------------------------
and soon...
Table2
----------------------------------
**ID** | **SUBJECT** | **GRADE** |
----------------------------------
001 | math | 80 |
----------------------------------
003 | english | 83 |
----------------------------------
and soon....
here is the problem, I just want to select a row from table1 were its id dont match in table2.
I used this condition but it wont work the way I expected to so that means this is wrong,
if($t1_id!=$t2_id){
blah blah blah...
}
else
{
all data is match!
}
when an id in table2 matched in table1 it already show that my data is all match even if there is only one entry in table2.
Please help me. If you don,t understand how I show my problem please tell me,THANK YOU!.
These 2 solutions will work in sqlserver:
Solution 1:
SELECT
t1.ID, t1.NAME, t1.AGE
FROM
table1 t1
LEFT JOIN
table2 t2
ON t1.ID = t2.ID
WHERE t2.ID is null
Solution 2:
SELECT
ID, NAME, AGE
FROM
table1 t1
WHERE NOT EXISTS(SELECT * FROM table2 WHERE t1.ID = ID)
You can use exists clause to check whether a record is present in another table, e.g.
select *
from table1 t1
where t1.id = ?
and exists(
select *
from table2 t2
where t2.id = t1.id;
);
Related
Hi, guys
Can't find answer in other topics, so asking here.
I have a Table in database
Table
------------------------------
id | name | last_name | created_by_id |
1 | Bilbo ..| Baggins.....| 0 .................... |
2 | Frodo . | Baggins.....| 1 ................... |
Is there any way i can get 1st row name value by using 2nd row created_by_id ?
I need to get sentence Frodo Baggins was created by Bilbo Baggins.
Can't find the right sql sentence
You need self join :
select t.*, t1.name, t1.last_name
from table t inner join
table t1
on t1.id = t.created_by_id
where t.id = 2;
you can just use a join
select *,<your string stuff here>
from <table> as a
inner join <table> as b
on a.id = b.created_by_id
Hi guys im have trouble with a SQL join statement. i cant seem to get it to work the way i want
ex.
TABLE1 TABLE 2
ID NAME ID INFO
1 JOE 1 YES
2 MIKE 1 NO
3 JESS 1 MAYBE
4 ROB 2 NO
2 NO
$stid = oci_parse($conn, "
SELECT * FROM TABLE1 TBL1 RIGHT JOIN TABLE2 TBL2
ON (TBL1.ID = TBL2.ID0 WHERE TBL1.ID = '1'
");
oci_execute($stid);
while (($row = oci_fetch_array($stid, OCI_BOTH+OCI_RETURN_NULLS)) != false) {
echo $row['NAME']."<br/ >";
echo $row['INFO']."<br/ >";
}
what i want to see is
RESULT
JOE
YES
NO
MAYBE
typically what i get (regardless of join type) is something like
JOE
YES
JOE
NO
JOE
MAYBE
any help would be great.
don't use rigth join, change it for left join
SELECT * FROM TABLE1 TBL1 LEFT JOIN TABLE2 TBL2
ON (TBL1.ID = TBL2.ID) WHERE TBL1.ID = '1'
First of all, you need to change the RIGHT JOIN to LEFT JOIN (or simple JOIN, check your requirements). Now for the main part of your problem: the JOIN works as expected, you need to use your favourite language to do something like that:
Pseudocode
var test_name = "";
var last_name = "";
while EXIST_MORE_RECORDS {
test_name = $row['NAME'];
if (test_name != last_name) {
last_name = test_name;
print last_name;
}
print $row['INFO']."<br/ >";
}
Look at the results of your query:
+----+------+----+-------+
| ID | NAME | ID | INFO |
+----+------+----+-------+
| 1 | JOE | 1 | MAYBE |
| 1 | JOE | 1 | NO |
| 1 | JOE | 1 | YES |
+----+------+----+-------+
Now look at your loop. You're outputting NAME and INFO for each row. You could alter your loop like #giorgos-altanis suggests.
You could also change your query to use UNION, and grab the NAME and INFO separately so that each is its own row.
select Name VALUE, 0 sortorder
from TABLE1
where ID = '1'
union
select Info VALUE, 1 sortorder
from TABLE2
where ID = '1'
order by sortorder
This would give you results like :
+-------+-----------+
| VALUE | sortorder |
+-------+-----------+
| JOE | 0 |
| MAYBE | 1 |
| NO | 1 |
| YES | 1 |
+-------+-----------+
Then you could use a simple loop. Hard to say whether this would be an appropriate solution to your actual code.
Note that the order of rows from Table2 cannot be guaranteed. If you want them to appear in order of YES, NO, MAYBE, you'll need an additional field in the table to define the sort order (or maybe a CASE statement).
UPDATE
If all you want is the count of the rows from Table2 (which is a completely different question), then you just need to join the tables and then group on the NAME:
select TBL1.ID, TBL1.NAME, COUNT(1) TBL2COUNT
from TABLE1 TBL1
join TABLE2 TBL2 on TBL1.ID = TBL2.ID
where TBL1.ID = '1'
group by tbl1.ID, TBL1.NAME
Example Table Structure
Table 1
ID | Name | Price
-----------------------------
1 | Casio | 30
2 | Titan | 40
Table 2
ID | Place | Price
-----------------------------
1 | Cali | 30
2 | Mexi | 10
Operation to perform:
Table1(Price) - Table2(Price) for ID = 1
New Table 1
ID | Name | Price
-----------------------------
1 | Casio | 0
2 | Titan | 40
ID matches in both tables
You should consider another database design to handle this case.
But to answer your question, you can create a view :
create view Differences2 as (
select t1.id, t1.price - t2.price
from t1, t2
where t1.id = t2.id
)
As you told both table will have same ID column you can use following query.
SELECT table1.ID, table1.Name, (table1.Price-table2.Price) AS Price
FROM table1
INNER JOIN table2 ON table1.ID = table2.ID
If you want to update record you can use following:
UPDATE table1
INNER JOIN table2 ON table1.ID = table2.ID
SET table1.Price = (table1.Price-table2.Price)
I want to show all the results from Table1 based on some select condition being true, and have a variable be set to 0 or 1 for each result from Table1 based on some satisfied condition with Table2.
SELECT * FROM Table1 WHERE Some_Condition=true
Foreach Table1.Name
SELECT IF(TID IS NULL, 0, 1) AS Variable FROM Table2
WHERE
Table2.Name=Table1.Name AND Table2.Val='p'
How can I make this all into one SQL call?
example call I would like to see is:
Table1:
+----+-------------------+
| ID | Name |
+----+-------------------+
| 1 | John |
+----+-------------------+
| 2 | Alan |
+----+-------------------+
Table2: So here Alan exists AND Val='p', not just existing
+-------+-----------+-----+
| TID | Name | Val |
+-------+-----------+-----+
| 1 | Alan | p |
+-------+-----------+-----+
SQL result I want from a SINGLE SELECT statement:
+------+----------+
| Name | Variable |
+------+----------+
| John | 0 |
+------+----------+
| Alan | 1 |
+------+----------+
A LEFT JOIN and a CASE statement may work for you. Please see query below.
SELECT A.Name AS item, (CASE WHEN B.Val='p' THEN 1 ELSE 0 END) AS Variable
FROM Table1 A LEFT JOIN Table2 B ON (A.Name=B.Name)
I think you just want a JOIN:
SELECT t2.Name, IF(Tt2.ID IS NULL, 0, 1) AS Variable
FROM Table2 t2 JOIN
Table1 t1
ON t2.Name = t1.Name
WHERE t2.Val = 'p' AND <some condition on t1> = true;
In MySQL, you can simplify the SELECT to:
SELECT t2.Name, (Tt2.ID IS NOT NULL) AS Variable
Note that I added the name to the SELECT, although it is not in your sample SQL.
You need LEFT JOIN Table2 to include all rows from Table1 even if row in Table2 not exists. And then in Variable column just check if Table2.TID is presented (i.e. not null).
SELECT Name, (Table2.TID IS NOT NULL) AS Variable
FROM Table1
LEFT JOIN Table2 ON Table2.Name=Table1.Name AND Table2.Val='p'
Or it can be done with IF():
SELECT Name, IF(Table2.TID IS NULL, 0, 1) AS Variable
FROM Table1
LEFT JOIN Table2 ON Table2.Name=Table1.Name AND Table2.Val='p'
I have 2 database tables:
Table 1:
+---------+-------+-------------+
| Page | Title | Description |
+---------+-------+-------------+
| Apple | ..... | ........... |
| Orange | ..... | ........... |
| Pear | ..... | ........... |
| Grapes | ..... | ........... |
+---------+-------+-------------+
Table 2:
+----------+-------------+
| Link | Page |
+----------+-------------+
| Website1 | Apple |
| Website2 | Orange |
| Website3 | Apple |
| Website4 | Orange |
| Website5 | Apple |
| Website6 | Pear |
| Website7 | Apple |
| Website8 | Grapes |
| Website9 | Grapes |
+----------+-------------+
I want to know/return how many pages from Table 1 are referenced in Table 2 and how many times they are referenced. (I DON'T want to know how many times EACH page in Table 1 is referenced in Table 2).
So in this example:
1 page is referenced 1 time (Pear),
2 pages are referenced 2 times (Grapes and Orange) &
1 page is referenced 4 times.
What kind of SQL statement would I use to get this?
Following query should do..
SELECT COUNT(1) NoOfPages,CNT ReferencedTimes
FROM
(
SELECT T2.PAGE,COUNT(1) CNT
FROM TABLE1 T1 INNER JOIN TABLE2 T2 ON T1.PAGE = T2.PAGE
GROUP BY T2.PAGE
)T
GROUP BY CNT
I think the following statement will fit:
SELECT count(*) FROM Table2 WHERE (Table2.Page IN (SELECT Page FROM Table1));
Use This query
Select table2.page,cnt(table2.page)
from table1 inner join table2
On table1.Page=table2.Page group by table2.page
SELECT (GROUP_CONCAT(DISTINCT page)) AS Page,page_count
FROM
(SELECT table1.Page as page,COUNT(*) as page_count
FROM table1 INNER JOIN table2 ON table1.Page=table2.Page
GROUP BY table1.Page)
as T GROUP BY page_count
Hope this helps
If what you are seeking is X page was referenced N times, the below query will achieve that:
SELECT COUNT(t1.page), t2.count
FROM table1 t1
INNER JOIN (SELECT page,COUNT(*) AS count FROM table2 GROUP BY page) t2 ON t1.page=t2.page
GROUP BY t2.count
Try this query, it will make a left join and tell you how many times item is referenced in table2, if count is zero than no reference in the other table
SELECT table1.Page, count(table2.Page) as count
FROM table1
LEFT JOIN table2 ON table2.Page = table1.Page
GROUP BY table1.Page