I have problems to relate the columns of two tables
this is my table1 structure
where I want to relate the identify column to the id of table 2
this is my table2 structure
this is my query
$id = (!empty($_GET['id']) ? $_GET['id'] : 0);
$example1 = (!empty($_REQUEST['example1']) ? $_REQUEST['example1'] : '');
$example2 = (!empty($_REQUEST['example2']) ? $_REQUEST['example2'] : '');
$consult = "SELECT a.id, a.user, a.date, a.action
FROM table1 a
INNER JOIN table2 b on b.idwork = $id
WHERE module = 'Activity ".$idexample1."|".$idexample2."' AND identifier = $id ORDER BY id DESC ";
I get this after run my query
SELECT a.id, a.user, a.date, a.action
FROM table1 a
INNER JOIN table2 b on b.id = 244
WHERE module = 'activity 1|98' AND identificador = 244 ORDER BY id DESC
what I try is to relate and make is that $id be the same identifier number
The join should be based ON the equality of the columns b.id and a.identifier:
SELECT a.id, a.user, a.date, a.action
FROM table1 a INNER JOIN table2 b
ON b.id = a.identifier
WHERE a.module = 'activity 1|98' AND b.idwork = 244
ORDER BY a.identifier DESC
I used also the WHERE clause from your code and changed the column names to the ones of your sample data.
Related
I need help on below statement. I need to put WHERE items.IID = 8 so that it shows only details pertaining to IID number 8. But when I use WHERE items.IID = 8, it is not working. I have to use this type of join as I want to do Sum and Count of some fields. There are 3 tables. 1st is items, 2nd is ItemPurchaseHistory and 3rd is ItemIssuedHistory.
SELECT items.IID, items.ItemName, ItemPurchaseHistorySum.SumOfUnitsPurchased, ItemPurchaseHistorySum.SumOfCost,
ItemPurchaseHistoryCount.CountOfUnitsPurchased,
ItemIssuedHistorySum.SumOfUnitsIssued
FROM items
LEFT JOIN (SELECT IID, SUM(UnitsPurchased) AS SumOfUnitsPurchased, SUM(Cost) AS SumOfCost
FROM ItemPurchaseHistory
GROUP BY IID) ItemPurchaseHistorySum ON ItemPurchaseHistorySum.IID = items.IID
LEFT JOIN (SELECT IID, Count(UnitsPurchased) AS CountOfUnitsPurchased
FROM ItemPurchaseHistory
GROUP BY IID) ItemPurchaseHistoryCount ON ItemPurchaseHistoryCount.IID = items.IID
LEFT JOIN (SELECT IID, SUM(UnitsIssued) AS SumOfUnitsIssued
FROM ItemIssuedHistory
GROUP BY IID) ItemIssuedHistorySum ON ItemIssuedHistorySum.IID = items.IID
WHERE item.IID = $_GET['id']
ORDER BY items.IID ASC
I have two tables tableA and tableB. Both have two similar columns ID and B_ID.
I want to check whether both table have similar values. My code is:
$ac = $mysql->query("(SELECT ID,B_ID FROM tableA) INTERSECT (SELECT ID,B_ID FROM tableB)");
if($ac){
while($row = $ac->fetch_assoc()){
echo "ID ".$row["ID"]." B_ID".$row["B_ID"]."<br>";
}
}
But this doesn't give any result.
ps: tableA(ID,B_ID)
1->23
2->23
3->23
4->56
5->67
tableB(ID,B_ID)
3->23
8->26
11->27
12->66
here both table has 3->23 but 1->23 2->23 is not in tableB how can i figure that sort of records. same B_ID but different ID
If you have B.ID column is present in both table then use following JOIN query
SELECT a.ID, a.B_ID
FROM tableA AS a
JOIN tableB AS b ON (a.B_ID = b.B_ID AND a.ID = b.ID)
Use a join to get the data and just iterate over your results. Run the query and if there is any record that satisfies the query or not.
select t1.ID, t1.B_ID from tableA t1, tableB t2
where t1.ID = t2.ID
and t1.B_ID =t2.B_ID
Use an INNER JOIN and COUNT
SELECT COUNT(*) as cnt
FROM tableA INNER JOIN tableB
ON tableA.B_ID = tableB.B_ID AND tableA.ID = tableB.ID
now if cnt is greater than zero than common values exist, otherwise no
Please try with this query may be help full.
SELECT
tbla.ID,
tbla.B_ID
FROM
tablea AS tbla,
tableb AS tblb
WHERE
tbla.B_ID = tblb.B_ID
Good day, I selected two MySQL tables- tcomment and members, but these two tables have similar fields called 'id' and now, I want to select the id of the 'tcomment' table but instead, the id of 'members' table shows up. How do I do this? This is the code below.
<?php
$comment= "SELECT c.*, m.* FROM tcomment c JOIN members m ON c.poster = m.id WHERE comment_id = :id";
foreach ($db->query($comment, array('id' => $_GET['id'])) AS $tresult){
echo "{$tresult['id']}";
}
?>
You could use aliases on the SQL query;
Sample Code
$comment= "SELECT c.*, c.id as comment_id, m.*, m.id as member_id FROM tcomment c JOIN members m ON c.poster = m.id WHERE comment_id = :id";
As #u_mulder said, apply aliases
$comment =<<<ccc
SELECT c.id AS 'idWanted', c.*, m.*
FROM tcomment c JOIN members m ON c.poster = m.id
WHERE comment_id = :id
ccc;
foreach ($db->query($comment, array('id' => $_GET['id'])) AS $tresult){
echo "{$tresult['idWanted']}";
}
table1
--------------
| sn | class |
--------------
table2
----------------
| id | student |
----------------
all are int as sn is table1 is linked to student in table2
sn, id are auto increasing. when inserting data to table2 student column is same as sn in table 1
now I want to select student in table2 but only those whose class in table1 is "3"
my syntax is thus;
$count = mysql_query(SELECT student from table2 whose class in table1 =3)
so that i can count them by saying
$quantity = mysql_num_rows($count)
now my problem is if sql also have this whose keyword, or how do i go about this.
$count = mysql_query(SELECT student from table2 whose class in table1 =3)
You need to join the tables in order to filter the results properly.
(1) This will give you the number of students for class 3.
$count = mysql_query(
'SELECT COUNT(t2.student)
FROM table2 t2
INNER JOIN table1 t1
ON t1.sn = t2.student
AND t1.class = 3'
);
(2) This will give you all classes and the number of students for each.
$count = mysql_query(
'SELECT t1.class, COUNT(t2.student)
FROM table2 t2
INNER JOIN table1 t1
ON t1.sn = t2.student
GROUP BY t1.class
ORDER BY t1.class'
);
(3) This will give you all classes and the students list.
$list = mysql_query(
'SELECT t1.class, GROUP_CONCAT(t2.student SEPARATOR ',')
FROM table2 t2
INNER JOIN table1 t1
ON t1.sn = t2.student
GROUP BY t1.class
ORDER BY t1.class'
);
You should join those two tables and limit your result to those which have table1.class = 3
SELECT
student
FROM
table2 a
JOIN table1 b ON (a.student = b.sn)
WHERE b.class = 3
If you want a count you could also do it through SQL by using aggregate function
SELECT
COUNT(student)
FROM
table2 a
JOIN table1 b ON (a.student = b.sn)
WHERE b.class = 3
I have drawn this image to explain what I need
1.to compare a user_id with the user_id's in two different tables
2.the corresponding ref_global_id from both the tables are then matched to a events table
3.matching global_id's from the events table are then arranged in ascending order.
Or this:
SELECT e.global_id, e.event_time
FROM (SELECT * FROM table1
UNION
SELECT * FROM table2) x inner join
event_table e ON e.global_id = x.ref_global_id
WHERE x.[user_id] = 121
SELECT e.global_id, e.event_time
FROM events_table e
JOIN table1 t1 on e.global_id = t1.ref_global_id
JOIN table2 t2 on e.global_id = t2.ref_global_id
WHERE t1.user_id = 121 AND t2.user_id = 121
ORDER BY e.event_time
Try this:
select global_id, event_time from event left join table1 on event.global_id = table1.ref_global_id AND table1.user_id = 121 left join table2 on event.global_id = table2.ref_global_id AND table2.user_id = 121