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']
Related
I know this involves JOINS but I can't seem to find a working solution to what I'm trying to do.
I have 2 custom tables :
table1 | table2
---------------------
id id
uid uid
track_id track_id
date date
art active
info
blah
blah2
First I want to select everything WHERE uid=55 AND active=1 from table2 :
$tracks = $wpdb->get_results( "SELECT * FROM table2 WHERE uid = 55 AND active = 1");
And then match the track_id from table2 with results from table1 so I can traverse the table1 data.
I know I can do it like this :
foreach( $tracks as $track ) {
$this_track = $track->track_id;
$results = $wpdb->get_results( "SELECT * FROM table1 WHERE track_id = $this_track");
// Do stuff here
}
But this is the part where it gets tricky...
I then want to ORDER the $results from table1 by date DESC from table2
And this is where I'm lost...
Effectively I want (pseudo code) :
$results = $wpdb->get_results( "SELECT * FROM table1 WHERE track_id = $this_track" ORDER BY date DESC FROM table2);
As well as that last bit, I know I can do this entire routine with JOINS to keep this all in one query and make it way more efficient but I just don't know how.
So just to be clear, my overall routine should be like this :
Get all instances of track_id from table2 where user_id=55 and active=1, then use those results to match the track_id to every result in table1 with the same track_id and then sort the results by date back over from table2
Psuedo code, I know it contains nonsense :
$finalresults = $wpdb->get_results( "SELECT * FROM table2 where uid=55 AND active=1 THEN SELECT * FROM table1 WHERE track_id = "the track_id from the first query" THEN ORDER BY date DESC FROM table2);
Try with this query
SELECT t1.* ,t2.date AS t2date, t2.active FROM table2 AS t2 INNER JOIN table1 AS t1 ON (t1.track_id = t2.track_id) WHERE t2.uid=55 AND t2.active=1 ORDER BY t2.date DESC;
Edit: Explanation of what this query is doing. and inverted the order of the tables retrieved in the query (this don't affect the final datatset, i did this to make to follow the logic of the explanation.
1.- Begin with retrieving all rows from table2 (theres is no specific reason because i used table2 over table1, I'm only following an logical order), using the criteria that you specified iud=55 and active=1
SELECT * FROM table2 WHERE uid=55 AND active=1;
2.- but as you said you need to expand the data retrieved in table2 with some information in table1, that's exactly what it is the directive JOIN made, and we are using INNER JOIN because this type of JOIN will show rows ONLY if data for the uid=55 is present on table1, if there is NO data for the uid=55 present on both TABLES then mysql wil show empty the recordset (0 Rows selected).
in the ON(...) part I specify which criteria mysql will use to compara both tables for match in this case will compare that track_id on table2 it is the same that the specified on table1, if this codition is met then mysql considers it as a match.
anly for convenience and because i'm adding a Second table i gave an Alias to each one t1 and t2.
then the query now seems like this
SELECT * FROM table2 AS t2 INNER JOIN table1 AS t1 ON(t1.track.id = t2.track_id) WHERE t2.uid=55 AND t2.active=1;
3.- but then raise a problem, both tables has rows with the same field names, and this is something that DBMS don't like in their queries, to avoid this situation in the query i only show the fields (id, uid and track_id) from one table in this case t1 (t1.*) and only show the fields that doesn't have this problem from t2 (t2.date AS t2date, t2.active). in this way mysql won't throw any error.
SELECT t1.* ,t2.date AS t2date, t2.active FROM table2 AS t2 INNER JOIN table1 AS t1 ON (t1.track_id = t2.track_id) WHERE t2.uid=55 AND t2.active=1;
4.- for the final step i specify to mysql that i want all found rows ordered descent by a field in the table2;
ORDER BY t2.date DESC;
then this criteria will be applied to the whole selected rows. and the final query has this form.
SELECT t1.* ,t2.date AS t2date, t2.active FROM table2 AS t2 INNER JOIN table1 AS t1 ON (t1.track_id = t2.track_id) WHERE t2.uid=55 AND t2.active=1 ORDER BY t2.date DESC;
if is not completely clear you can ask ...
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
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 am using this code:
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
Then to count the number of records return:
$num_rows = mysql_num_rows($result);
echo "$num_rows";
It works great but venue_id in Table1 has lots of entries and I only want it to get one per venue_id
How can I make it so it only returns 1 venue_id instance?
Use GROUP BY clause,
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
GROUP BY `Table1`.`venue_id`
I am trying to get a query using fields from 2 tables.
I need to query Table1 but only Table2 has the variable venue_location that I need to query.
Basically I need to count all records on Table1 where Table1.venue_location = $MyVariable.
Here is what I've put together but I believe I need to use Joins for this?
Table1
- venue_id
Table2
- venue_id,
- venue_location
SELECT * FROM Table1 WHERE table1.venue_id = table2.venue_id and table2.location = '$MyVariable'
How can I do a query for this?
Use the power of join table
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
You can get back the count of rows with mysqli_num_rows() in PHP, or change le select by SELECT COUNT(*) AS nbRow FROM ... and check of value in nbRow column
You can join two tables on venue_id and then group it by venue_id where location is your $MyVariable.
Your final query will look like:
SELECT count(table2.venue_id)
FROM Table1
JOIN Table2 ON table1.venue_id = table2.venue_id
WHERE table2.location = '$MyVariable'
GROUP BY table2.venue_id
try this
SELECT Table1.venue_id, Table2.venue_location FROM Table1 INNER JOIN Table2
ON Table1.venue_id='$MyVariable';