i am trying to achieve the following result. (i am sorry for the horrible explanation but i find it very hard to explain :P)
I need data from 2 tables.
Table1
id, table2_id, user_id
Table2
id, Name
Table1 example information
ID 1 Table2_id 1 user_id 3
ID 2 Table2_id 2 user_id 3
ID 3 Table2_id 5 user_id 3
Table2 Example Information
ID 1 Name TEST
ID 2 Name Hello
ID 3 Name Helpme
ID 4 Name Please
ID 5 Name IamLost
i Would like to output everything tied user_id 3. This would be my ideal end result
TEST
Hello
IamLost
I have this as code
$id = "3";
$sth = $db->prepare("SELECT table2_id, user_id, naam FROM table1, table2 where user_id = $id ");
$sth->execute();
$result = $sth->fetchAll();
foreach( $result as $row ) {
echo $row['table2_id'] . ' ';
echo $row['naam'] . '<br>';
}
But this just outputs everything but then twice. like this
TEST
TEST
Hello
Hello
Helpme
Helpme
Please
Please
IamLost
IamLost
A LEFT JOIN should do the trick:
SELECT `table1`.`table2_id`, `table1`.`user_id`, `table2`.`name`
FROM `table1`
LEFT JOIN `table2`
ON `table1`.`Table2_id` = `table2`.`id`
WHERE `table1`.`id` = $id
MySQL JOIN Syntax
Use Joins in SQL.
The SQL Query should look like this:
SELECT T1.USER_ID, T2.NAME
FROM TABLE1 AS T1
JOIN TABLE2 AS T2
ON T1.TABLE2_ID = T2.ID
WHERE T1.USER_ID = 3
these two table must be related to each other. When you select , returned rows should be equal this two tables. This why we use table joins e.g
SELECT a.user_id,a.table_id,b.name FROM table1 as a, table2 as b
RIGHT OUTER JOIN table 1
ON b.ID = a.table2_id
AND table1.user_id = 3
I believe you just need to be more precise:
$sth = $db->prepare("SELECT table2_id, user_id, name
FROM table1
LEFT JOIN table2
ON table1.Table2_id = table2.id
WHERE user_id = $id ");
A simple right outer join will help you here.
SELECT * FROM table2
RIGHT OUTER JOIN table 1
ON table2.ID = table1.table2_id
AND table1.user_id = 3
Related
How get selected rows where user id is not in table record
the table like this
table_a
id subid userid
1 2 123
2 4 123
table_b
id title
1 like
2 liked
3 bookmark
4 bookmarked
i use join table to get the result
if WHERE userid=123 then get result
id title
2 liked
4 bookmarked
the query
select b.id,b.title from
table_a a
LEFT JOIN table_b b
ON b.id = a.subid
WHERE a.userid = '123'
but, how if userid = '345' and not in table_a get the result too?.. the result is must like this (selected rows)
id title
1 like
3 bookmark
Thankyou
You can use the following query:
SELECT id, title FROM t2 LEFT JOIN t1 ON t1.subid = t2.id
WHERE (t1.userid = 345 AND NOT userid IS NULL) OR (
NOT EXISTS(SELECT userid FROM t1 WHERE userid = 345) AND userid IS NULL
);
The working examples you can find here: http://sqlfiddle.com/#!9/c6a446/25
Try using this query
select a.no, b.title from a join b on (b.no=a.no) where a.userid='345'
You can use left join and get the data for the table where their is no match. I have solved this recently.
Chk this link:
Least Popular Event - Get list of events where bookings IS NULL OR LEAST
In mysql
SELECT b.no,b.title FROM a LEFT JOIN b ON a.subid = b.no and where a.userid IS NULL;
SELECT b.no,b.title FROM a LEFT JOIN b ON a.subid = b.no and where a.userid <> '345';
in laravel :
$least = bmodel::leftJoin('a', function($join)
{
$join->on('a.subid', '=', 'b.no');
})
->select('b.no','b.title')
->whereNull('a.userid')
->first(); or get()
You can use this query to get one or more results. It basically compares userid column with no column and gets list of no and title where there is no match in table a.
This basically would select with given userId or if that is not present it then selects the result which
SELECT * FROM t2 LEFT JOIN t1 ON t1.subid = t2.id WHERE t1.userid = 123||
(select userid from t1 where userid =123LIMIT 1) is null
This is the second case where you will get all the rows where it is not 123 or any number
SELECT * FROM t2 LEFT JOIN t1 ON t1.subid = t2.id WHERE t1.userid = 345 ||
(select userid from t1 where userid =345 LIMIT 1) is null
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 would like to join two tables and only print records from table 1 where the rec_number is NOT in table 2.
table 1
name rec_number
john smith 123
Bob jonson 345
etc
Table 2
Bob jonson 345
etc
What is the query in php that would do this so the query only gives me John smith, not bob jonson.
is it:
$query = "select * from table1
left join rec_number on table1.rec_number = table2.rec_number";
$result=mysql_query($query);
Thank you.
You can use this query
select
t1.*
from table1 t1
left join table2 t2
on t2.rec_number = t1.rec_number
where t2.rec_number IS NULL
Beside the left join mentioned by Abhik, you could also use a subselect:
SELECT * FROM table1 WHERE table1.name NOT IN (SELECT name FROM table2);
I need to query the database to pull all cars that a particular user likes.
I know that user_id is 1
Then I have 2 tables. 1 contains all the cars ... id and description and table 2 contains the likes.
Table 1 has a list if cars and these fields:
car_id
car_name,
car_description
Table 2 has what cars I like and these fields:
user_id
car_id
likes (1 or 0)
So I need to pull out only the records that user 1 likes from Table 2 but only the ones he likes
What SQL query would I need to do for that?
SELECT * FROM table1 as t0
LEFT JOIN table2 as t1 on t0.car_id = t1.car_id
WHERE t1.likes = 1
Try this
SELECT * FROM table1 as t1 LEFT JOIN table2 as t2 on t1.car_id = t2.car_id WHERE t2.user_id = $user_id
Try this query
SELECT t1.*,t2.*
FROM tbl1 t1,tbl2 t2
WHERE likes = 1 AND user_id = 1 AND t1.car_id = t2.car_id
I need to select all (*) the rows from table1, table2, table3.. but I need to select the MIN and MAX price from table 2 within this INNER JOIN. I've read up on how to do this, but how do I do this within an INNER JOIN, and how do I display it in a PHP variable.
Initial Problem: How do I display the min and max values once I pull them.. (e.g $Result['MinPrice'], $Result['MaxPrice']).
Here's my query:
$Query = mysql_query("
SELECT *
FROM table1
INNER JOIN table2 ON table1.UserID = table2.UserID
INNER JOIN tables3 ON table2.DeviceID = table3.DeviceID
WHERE table2.DeviceID = '$GetDeviceID'
");
Here is the tables structure:
table1 = usersinfo
UserID UserFirstName UserLastName UserDisplayName
1 John Doe John D.
table2 = listings
ListingID UserID DeviceID
11 1 2
table3 = devices
DeviceID
2
If you really want to do what you're asking in this way you can use the query that is displayed below. This does, however, return a lot of duplicate rows if you have multiple rows returned when querying. Try it and see if it works.
$Query = mysql_query("
SELECT table1.*, table2.*, table3.*,
MIN(table2.price) as minny,
MAX(table2.price) as maxxy
FROM table1
INNER JOIN table2 ON table1.UserID = table2.UserID
INNER JOIN tables3 ON table1.DeviceID = table3.DeviceID
WHERE table1.DeviceID = '$Something'
GROUP BY table2.ListingAskingPrice
");
Then get this value by doing $result['minny'] and $result['maxxy']