Selecting one row value based on anothers row value - php

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

Related

SQL JOIN not working the way i need

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

Selecting row from two table

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

sql where clause for each row of a subquery

I have 2 tables in MSSql
table_a:
data|id_1|timeInIntForm
------------------------------
data1 | 1/22323/3 | 1433721600
data2 | 1/22323/3 | 1433721660
and I have another table
table_b
data|(string list of ids)|startTimeinIntform|EndTimeInIntForm
--------------------------------------------------------------
dataA| (a_1223233_z a_1223233_x) | 1433721601 | 1433721659
datab| (a_1223233_z a_1223233_x) | 1433721602 | 1433721645
I want to do a
select * from table_a where id_1 = 'someId'
and timeInIntForm between 'time1' and 'time2'
and not between TableB_row1[startTimeinIntform] and TableB_row1[EndTimeInIntForm]'
.
.
.
and not between TableB_rowN[startTimeinIntform] and TableB_rowN[EndTimeInIntForm]'
The ids do not match but can be correlated on a different table that has
table_c:
data|id_1|id_2
-----------------------------
data| 1/22323/3 | a_1223233_z
data| 1/22323/4 | a_1223233_x
my initial thought is in PHP to get the rows from table_b and then as I select from table_a build the not between clause from each row in table_b. But I would LOVE to do this in one select statement if possible. Any ideas?
I edited it to have some "sample" data I was asked for. I am stumped on how to join these .. I admit freely I am not a pro or even poor at joins but I am really stumped on how to join table b to a and c
Well I am not sure exactly without the tables but I think this would do it.
Select a.*
From table_a a
join table_c c
on a.ID_1 = c.ID_1
join table_b b
on c.ID_2 = b.ID_2
Where a.ID_1 = 'someid'
and a.timeinintform not between 'time1' and 'time2'
and a.timeinintform not between b.starttimeininform and b.endtimeinintform

Selecting unique id in mysql

Below is the replicas of the tables I have created. My goal is to simply pick the unique id_num from the First Table which is not found on the Second Table.
I have tried doing the code below but somehow, I kept getting empty results
SELECT `first_table`.name FROM `first_table`
INNER JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `first_table`.name = `second_table`.name
First Table:
id_num | name
301 | Bobby
123 | George
25 | Vicky
Second Table:
id_num | name
301 | Bobby
435 | George
25 | Vicky
My desire result I am looking for:
id_num | name
435 | George
LEFT JOIN should work here.
SELECT `first_table`.name FROM `first_table`
LEFT JOIN `second_table`
ON `first_table`.id_num = `second_table`.id_num
WHERE `second_table`.id_num is NULL
See also this useful infographic
try this using NOT IN
select `id_num` , name from `table2` where name not in (
SELECT t1.name FROM `table1` t1
INNER JOIN `table2` t2
ON t1.id_num = t2.id_num )
DEMO HERE

Join table with multiple rows to one row

I have a database that looks like:
TABLE 1
ID | NAME | PRICE
TABLE 2
TABLE1.ID | ITEM
As you can see it is possible that table 2 can contain multiple references to table 1.
Is it possible to create a query that gives a result like this?
TABLE1.ID | NAME | PRICE | TABLE2.ITEM REC 1 | TABLE2.ITEM REC 2 | TABLE2.ITEM REC 3
Consider looking at this MySQL function: GROUP_CONCAT(expr). It will sure answer your question
Mysql Documentation - group_concat()
Try this one:
SELECT t1.*, GROUP_CONCAT(t2.ITEM) AS Items
FROM Table1 t1
JOIN Table2 t2
ON t1.ID = t2.TABLE1_ID
GROUP BY t1.ID
See this SQLFiddle

Categories