table user:
id_u* f_name l_name
----------------------
1 andi mitchel
2 sarah bench
3 kirsty larx
table voucher:
id_v* id_user id_target
1 1 2
2 2 3
quite confused how to join those table with two foreign keys
$db->query("SELECT * FROM voucher v
LEFT JOIN user u ON u.id_u = v.id_user
LEFT JOIN user u1 ON u1.id_u = v.id_target
WHERE .... ")
echoing while loop... and returns nothing??
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
echo $r['u.f_name'];
echo $r['u1.f_name'];
endwhile;
Your JOIN seems absolutely correct. The only issue is that you have joined table user twice, therefore you have columns with same name (like f_name). The database will assign different (but arbitrary) names to these columns. You can override this behaviour with the AS keyword:
$db->query("SELECT v.*
, u.f_name AS user_f_name
, u.l_name AS user_l_name
, ta.f_name AS target_f_name
, ta.l_name AS target_l_name
FROM voucher v
LEFT JOIN user u ON u.id_u = v.id_user
LEFT JOIN user ta ON ta.id_u = v.id_target
WHERE .... ")
Then:
while($r = $q->fetch_array(MYSQLI_ASSOC)) :
echo $r['user_f_name'];
echo $r['target_f_name'];
endwhile;
And I think you can replace the LEFT JOINs with (inner) JOINs. Unless you have id_user or id_target values referencing non-existing userids (id_u).
It looks like you are asking for all people who are in the voucher table regardless of them being in position 1 (user) or position 2 (target)... Then, showing that person's name.
This query does a pre-query of each possible person and their position basis (via WhichPosition).
SELECT STRAIGHT_JOIN
AllVoucherUsers.WhatPosition,
u.*
FROM
( select distinct
v.id_user,
'1' as WhatPosition
from voucher v
union select distinct
v.id_target as id_user,
'2' as WhatPosition
from voucher v
) AllVoucherUsers
join users u
on AllVoucherUsres.id_user = u.id_u
If you only want ONE instance of a given person -- REGARDLESS of their position, just strip out all instances of the "WhatPosition" reference...
SELECT STRAIGHT_JOIN
u.*
FROM
( select distinct
v.id_user
from voucher v
union select distinct
v.id_target as id_user
from voucher v
) AllVoucherUsers
join users u
on AllVoucherUsres.id_user = u.id_u
SELECT * FROM voucher v
LEFT JOIN user u ON u.id_u = v.id_user OR u.id_u = v.id_target
WHERE ....
how about:
SELECT * FROM voucher JOIN user ON id_user = id_u
Simpler still:
SELECT * FROM voucher, user WHERE id_user = id_u
Related
I have a PHP page like this:
ID
Name
Hours Flown
1
Joao
7
2
Andre
10
3
Tiago
15
And I want that "Hours Flown" column show a value from one column from a SQL table (users) with a sum of another SQL table (flights) where ID matches the "users" table.
I have a while($row = $result_list_pilots->fetch_assoc()) to show the table if that helps...
SQL query for now is a bit simple "SELECT * FROM users ORDER BY name;". Already tried some JOIN examples but without success as the imported_hours gets duplicated as result from flights table is being found.
Tables:
[Tables]
How to do this?
I think here is what you want :
SELECT
u.ID
, u.Name
, SUM(ISNULL(f.HoursFlown,0)) + MAX(Imported_hours) 'Hours Flown'
FROM Users u
LEFT JOIN Flights f
ON u.ID = f.User_id -- or whatever the fk is
GROUP BY u.ID, u.Name
SELECT u.ID, u.Name, SUM(f.HoursFlown)
FROM Users u
INNER JOIN Flights f ON u.Name = f.Name
GROUP BY u.ID, u.Name
My tables
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY id DESC
LIMIT 1";
This is my SQL query, my task is to show the last records from 3 tables, but the table is blank, I don't know why,thanks in advance people :)
I guess the problem is coming from the ORDER BY id DESC .
Indeed, you have no column so called id.
You should probably remove this clause, in order to make your code work.
If you want to take the last records anyway, you can put an ORDER BY address_id DESC which will do the job !
The code directly edited :
$sql="SELECT *
FROM addresses
LEFT JOIN users ON address_id = user_id
LEFT JOIN notes ON note_id = user_id
ORDER BY adress_id DESC
LIMIT 1";
This may work:
SELECT a.address_id, u.user_id, n.note_id
FROM addresses a
LEFT JOIN users_addresses ua ON ua.ua_address_id = a.address_id
LEFT JOIN users u ON u.user_id = ua.ua_user_id
LEFT JOIN notes n ON n.note_user_id = u.user_id
ORDER BY a.address_id DESC
LIMIT 1
Here is the query to get all data from all the tables, not sure what do you mean last records from 3 tables, I can see four tables there:
SELECT *
FROM `addresses`
LEFT JOIN `users_addresses` ON `users_addresses`.`ua_address_id` = `addresses`.`address_id`
LEFT JOIN `users` ON `users`.`user_id` = `users_addresses`.`ua_user_id`
LEFT JOIN `notes` ON `notes`.`note_user_id` = `users`.`user_id`;
I have 3 table: user , company and deal.
One user may own several companies. Deal is made between the 2 companies. I need a list of deals, which involved my company.
Deals must contain the following fields: partner_company_id,my_company_id,partner_photo,partner_name,deal_about.
Language code: PHP.
Database: Mysql.
1.List of my company I can get by user ID.
user_id = 22;
companyList = query('SELECT company_id FROM company WHERE user_id = ?', user_id);
2. Then i get deal list where my_company_id is company_first_id
list1 = query('SELECT u.name AS partner_name, u.photo AS partner_photo, d.first_company_id AS
my_company_id , d.second_company_id AS partner_company_id,d.about AS deal_about FROM deal AS d
INNER JOIN company AS c ON c.company_id = d.second_company_id
INNER JOIN user AS u ON u.user_ud = c.user_id
WHERE d.company_first_id IN (?)', companyList);
3. Then i get deal list where my_company_id is company_second_id
list2 = query('SELECT u.name AS partner_name, u.photo AS partner_photo, d.first_company_id AS
partner_company_id , d.second_company_id AS my_company_id,d.about AS deal_about FROM deal AS d
INNER JOIN company AS c ON c.company_id = d.first_company_id
INNER JOIN user AS u ON u.user_ud = c.user_id
WHERE d.company_second_id IN (?)', companyList);
4. then i marge to array and set limit list
list = array_marge(list1,list2);
result = array_slice (list ,0 , 10);
HELP please optimize this queries.
THANKS.
DATABASE SCHEME
user | company | deal |
--------------------------------------------------
user_d | company_id | deal_id
photo | user_id |first_company_id
name | about |second_company_id
| |description
Are your queries so slow? They don't look slow (provided you have indexes on all IDs of course).
However, you can save one database access by combining the two deal queries. Either you simply select query1 UNION ALL query1 or you do it in one pass:
select
u.name AS partner_name,
u.photo AS partner_photo,
d.my_company_id,
d.partner_company_id,
d.about AS deal_about
from
(
select
about,
case when company_first_id in (?) then
company_first_id
else
company_second_id
end as my_company_id,
case when company_first_id in (?) then
company_second_id
else
company_first_id
end as partner_company_id
from deal
where company_first_id in (?) OR d.company_second_id in (?)
) as d
inner join company as c on c.company_id = d.partner_company_id
inner join user as u on u.user_ud = c.user_id
I have a LIKE table and a BOOK table and my user_id.
I want to pull only i have liked from BOOK table with BOOK NAMES and AUTHOR
My json will:
{book_id:1,bookname:sample,author:sean,user_id:111}
tables
-----------BOOK TABLE------------
ID ---- BOOK AUTHOR ---- BOOK NAME
1
2
...................................
USERS TABLE
ID-------NAME
1
2
.............
LIKE TABLE---------------------------
ID-----BOOK ID-------LIKER USER ID---
1
2
.....................................
$stmt = $mysqli->prepare("SELECT book.*, users.*, like.* FROM like INNER JOIN
users ON like.liker_user_id = users.id INNER JOIN
book ON like.book_id = book.id WHERE
users.id = ?
");
$stmt->bind_param( "d", $user_id);
$stmt->execute();
$stmt->bind_result($col1);
// then fetch and close the statement
For getting all the data related to likes you can run this sql command.
The result set you can loop and generate the JSON.
Make sure in the query you have all your column names correctly given.
select
b.book_id,
b.bookname,
b.author,
u.user_id
from
`like` l
inner join book b on b.book_id = l.book_id
inner join users u on u.user_id = l.user_id
If you need to filter data just add a where condition after the last JOIN as
where u.user_id = {your user id}
Here are my different tables:
computers (id,name)
monitors (id,name)
computer_monitor (id, computer_id,monitor_id)
useractivity (id,userid,timestamp,computer_monitor_id,ip)
useropinion (id,userid,computer_monitor_id,timestamp,rating)
user (id,name,email)
I want to search after the name of computer or monitor and get a row like this in return:
computer name and/or monitor name
computer_monitor_id
avg(rate)
count(useractivity)
avg(rate) is on that specific computer_monitor_id that matches the name, the same goes for count.
A computer with no connection to monitor has a value of 0 on monitor field in computer_monitor table and vice versa for monitor->computer.
useractivity and useropinion only contains the ID from computer_monitor table
As I understand, the query should be built around the computer_monitor table. All other tables connect to it, including those from which you want to obtain the stats.
SELECT
c.name AS ComputerName,
m.name AS MonitorName,
uo.AverageRating,
ua.ActivityCount
FROM computer_monitor cm
LEFT JOIN computer c ON c.id = cm.computer
LEFT JOIN monitor m ON m.id = cm.monitor
INNER JOIN (
SELECT computer_monitor_id, AVG(rating) AS AverageRating
FROM useropinion
GROUP BY computer_monitor_id
) uo ON cm.id = uo.computer_monitor_id
INNER JOIN (
SELECT computer_monitor_id, COUNT(*) AS ActivityCount
FROM useractivity
GROUP BY computer_monitor_id
) ua ON cm.id = ua.computer_monitor_id
Actually, as you can see, useropinion and useractivity are aggregated first, then joined. This is to avoid the Cartesian product effect when a computer_monitor.id matches more than one row both in useropinion and in useractivity.
<?php
$res_comp = mysql_query("select * from computers where name = '$name'");
$res_monitor = mysql_query("select * from monitor where name = '$name'");
if(mysql_num_rows($res_comp) > 0)
{
$row_comp = mysql_fetch_array($res_comp);
$comp_id = $row_comp['id'];
$res_result = mysql_query("select computers.name, computer_monitor.id, count(computer_monitor_id) from computers, computer_monitor, useractivity where computers.id = '$comp_id' AND computer_monitor_id = '$comp_id' AND useractivity.computer_monitor_id = '$comp_id'");
}
// repeat the same for monitor also. then use mysql_fetch_array to show your data.
?>
hopefully this will help.
This might do the trick...(one table with the computer/monitor relation ship, the other with a xref table threw me, and check the join types depending on your data)
SELECT computers.name AS ComputerName
, monitors.name AS MonitorName
, AVG(useropinion.rating) AS AvgRating
, COUNT(useractivity.id) AS ActivityCount
FROM computers
INNER JOIN computer_monitor ON (computers.id = computer_monitor.computer_id)
INNER JOIN useractivity ON (computers.id = useractivity.computer_id)
INNER JOIN monitors ON (computer_monitor.monitor_id = monitors.id)
INNER JOIN useropinion ON (computer_monitor.id = useropinion.computer_monitor_id) AND (monitors.id = useractivity.monitor_id)
INNER JOIN USER ON (useropinion.user_id = user.id) AND (useractivity.user_id = user.id)