Can I get data from a table based on multiple other tables? - php

$query = "SELECT * FROM table3 WHERE name_id = '(SELECT name_id FROM table2
WHERE salary < 1000 && name = '(SELECT name FROM table1
WHERE savings > 1000)')'";
Basically I want to get the data from the table1 based on the savings and use it to get the data from table 2 and use that data to get all the information from table 3. But this wont work. Is my code right or am I doing something wrong?
I also cannot create new tables, I simply want to display the data from table 3.

Use join
SELECT * FROM table3 t3 join table2 t2
on t3.name_id=t2.name_id
join table1 t1
on t3.name=t1.name
where salary < 1000 and savings > 1000

$query="SELECT * FROM table3 LEFT JOIN table2 ON table3.name_id=table2.name_id
LEFT JOIN table1 ON table3.name=table1.name
WHERE table2.salary < 1000 AND table1.savings > 1000 "

Another syntax of join is
SELECT * FROM table1 t1,table2 t2 ,table3 t3
where t1.name = t3.name and
t2.name_id = t3.name_id and
t1.savings > 1000 and t2.salary < 1000;

$query = "SELECT t3.* FROM table3 t3
INNER JOIN table2 t2 ON t2.name_id = t3.name_id AND t2.salary < 1000
INNER JOIN table1 t1 ON t1.name = t2.name AND t1.savings > 1000";

Related

MySQL two-table INNER JOIN , LEFT JOINED onto third table with only one row with lowest value

I searched around and found a near example to what I'm looking for, but it doesn't work in my case.
I have a query that does an INNER JOIN on two tables and this join constrains my overall data set substantially. I then want to LEFT JOIN onto a third table but I only want one record from that third table. The reason for the left join is because not every result of the INNER JOIN has a match in the 3rd table. Something like this:
SELECT DISTINCT t1.code, t2.id, t2.code, t3.id, t3.source_title, t3.display_order
FROM table1 t1
INNER JOIN table2 t2 ON t2.code=t1.code AND t2.type=0
LEFT JOIN table3 t3 ON t3.code=t1.code
ORDER BY t1.code, t3.display_order
This query returns too many records because the third table contains multiple records with a matching code. I just want the first one that matches with the lowest display_order value and, unfortunately, I can't limit the records to have display_order=1 because the lowest display order is not always one.
IMPORTANT: The t3.id value (if any) returned by this query must correspond to the record with the lowest display_order value. I.e., it won't work if the query correctly returns the lowest display_order value but the t3.id value corresponds to some other record in table 3.
Is this even possible? Any help would be much appreciated.
EDIT: Per Nick's suggestion, I have tried this, which appears to be working. I'll do some verification and report back:
SELECT DISTINCT t1.code, t2.*, sq.id, sq.source_title, sq.display_order
FROM table1 t1
INNER JOIN table2 p ON t2.code=t1.code AND t2.type=0
LEFT JOIN (
SELECT t3.*
FROM table3 t3
WHERE t3.display_order=(
SELECT MIN(display_order)
FROM table3 t3a
WHERE t3a.code = t3.code
)
) sq ON sq.code=t1.code
ORDER BY t1.code, sq.display_order
You should be able to replace table3 in your LEFT JOIN with
(SELECT *
FROM table3 t3
WHERE display_order = (SELECT MIN(display_order)
FROM table3 t3a
WHERE t3a.code = t3.code)
) t3
In MySQL 8.0 you can try to use row_number() for each code and ordered by display_order in a subquery from table3. Then left join that result and check for the row_number() to be equal to 1.
SELECT DISTINCT
t1.code,
t2.id,
t2.code,
t3.id,
t3.source_title,
t3.display_order
FROM table1 t1
INNER JOIN table2 t2
ON t2.code = t1.code
LEFT JOIN (SELECT t3.id,
t3.source_title,
t3.display_order,
t3.code,
row_number() OVER (PARTITION BY t3.code
ORDER BY t3.display_order) rn
FROM table3 t3) t3
ON t3.code = t1.code
WHERE t2.type = 0
AND t3.rn = 1
ORDER BY t1.code,
t3.display_order;
In lower versions you can try correlated subqueries ordered by display_order and LIMIT 1 (to get only one record).
SELECT DISTINCT
t1.code,
t2.id,
t2.code,
(SELECT t3.id
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) id,
(SELECT t3.source_title
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) source_title,
(SELECT t3.display_order
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1) display_order
FROM table1 t1
INNER JOIN table2 t2
ON t2.code = t1.code
WHERE t2.type = 0
ORDER BY t1.code,
(SELECT t3.display_order
FROM table3 t3
WHERE t3.code = t1.code
ORDER BY t3.display_order,
t3.id
LIMIT 1);
I assumed, that display_order in table3 isn't unique but id is. So I added id to the ORDER BY clauses in the subqueries to make sure the same record is selected in each of them. If display_order is unique, you can remove id FROM the ORDER BY clauses.
Edit:
If you don't want to repeat the subqueries in the (overall) ORDER BY clause, you can also order by the column ordinals. E.g.:
...
ORDER BY 1, 6;

Mysql 2 tables use player_id with name from other table

I am new to php & mysql and I'm trying to make a script that gets the distance walked with the player's name. I can get the player's walked distance with his id, but the value for the player_id is in a different table.
It looks like this:
Table1: player_id | foot (walked distance)
Table2: name | player_id
So I want to use the name by the player_id in my table.
Code
You require a simple join.
SELECT Table1.foot, Table2.name
FROM Table1
INNER JOIN Table2
ON Table1.player_id = Table2.player_id;
You just need to join both these table.
Just try this code:
$query = "SELECT T1.*, T2.name
FROM table1 T1
LEFT JOIN table2 T2 ON T1.player_id = T2.player_id
ORDER BY T2.name ASC";
For more details of JOIN: Link
Let me know for more help.
You can use
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id"
If you want to order the player names in alphabetical order then you can additionally use order by clause
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id
order by t2.name"
Use left join in mysql.
Suppose if you have two tables use this
SELECT T1.*,T2.walked distance
FROM table1 T1
LEFT JOIN table2 T2
ON T1.id=T2.player_id;
Click Here For more example

Search Function is Hang with 2 year data and join

here us the query
SELECT *
FROM Table1
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
AND order_id IN
(SELECT DISTINCT t1.order_id
FROM Table2 t1
INNER JOIN table3 t2 ON t1.prod_id = t2.prod_id
WHERE t2.prod_sku LIKE '%D-600%'
AND t1.create_dttm > '2013-02-15 08:28:41')
You are using a sub-query in WHERE clause, that could be the main reason behind slow execution of your query. Try using JOINS instead of sub query.
SELECT t1.*
FROM Table1 t1
INNER JOIN Table2 t2 ON T1.order_id = T2.order_id
AND t2.create_dttm > '2013-02-15 08:28:41'
INNER JOIN table3 t3 ON t2.prod_id = t3.prod_id
AND t3.prod_sku LIKE '%D-600%'
WHERE complete='Y'
AND shipped='Y'
AND active='Y'
AND create_dttm > '2013-10-10 08:28:41'
And also check for indexes on your tables.

PHP SQL output issue adding a third table to a query

I am currently using this script below.
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
WHERE table2.user_id = $userid");
The tables have these fields:
Table1:
id, venue_id, user_id...
Table2:
id, venue_id, user_id...
The query above returns 5 records.
Now....
I need to add a third table to the above script Table3
Table 3 fields also contains id, venue_id, user_id... BUT I don't what it in the WHERE of the script.
I've tried adding a LEFT JOIN to the script above to add the third table like this:
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
LEFT JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid");
The Stats table only contains 1 record.
Now, my problem is that the query above it's echoing the data on ALL the records and not just the one.
My question is...What I'm I doing wrong on the line I added:
LEFT JOIN stats as table3 on table1.venue_id = table3.venue_id ?
OK, I think you want to also join on the user_id. So
SELECT
*
FROM
`venues` AS table1
LEFT JOIN `follows` AS table2 USING (venue_id)
LEFT JOIN `stats` AS table3 USING (venue_id, user_id)
WHERE
table2.user_id = $userid
is my solution
If you only want to include record from table1 that have non null records in table3 then you need to use INNER JOIN and not a LEFT JOIN. See the MySQL documentation for JOIN
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
INNER JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid");
The "INNER" is not needed explicitly. Joins are INNER joins by default
You're not limiting the records at all. Use this instead:
$query = mysql_query("SELECT * FROM `venues` as table1
LEFT JOIN `follows` as table2 on table1.venue_id = table2.venue_id
LEFT JOIN `stats` as table3 on table1.venue_id = table3.venue_id
WHERE table2.user_id = $userid AND table3.venue_id IS NOT NULL");
You can use an INNER JOIN rather than an LEFT JOIN. See this SO post to make it clear or this blog article

How can I build this query to compare a id with two tables?

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

Categories