Im having some issues getting the result I want from two tables
table #1: history
customer_id | Action
------------------------
217 | buy
------------------------
218 | sell
------------------------
219 | hold
------------------------
table #2: Customers
customer_id | name
----------------------------
217 | Alan
----------------------------
218 | Jan
----------------------------
219 | Rick
I have a really long query now, but essentially I want to add to match the name with the amount. I tried this but it didn't work:
(SELECT action AS action FROM "history` LEFT JOIN ON " customer(customer_id=customer_id)`)
I'm not really familiar with doing queries so any help would be appreciated
It should be this:
SELECT h.Action AS action
FROM history h
LEFT JOIN Customers c
ON h.customer_id = c.customer_id
You either need to specify the tables or create an alias with which to associate columns/data.
Is a simple join
select action
from history
left join Customers on Customers.Customer_id = history.customer_id
and you can confirm using
select history.customer_id, Customers.Customer_id history.action , Customers.name
from history
left join Customers on Customers.Customer_id = history.customer_id
You can JOIN tables like this:
SELECT history.action AS Action ,Customers.name AS Name
FROM `history`
LEFT JOIN `Customers` ON history.customer_id = Customers.customer_id;
Related
So I have the following tables
**accounts**
id | iban
1 | ES80 2310 0001 1800 0001 2345
2 | ES91 2100 0418 4502 0005 1332
**acc_rel**
account_id | target_table | target_id
1 | users | 2
2 | clients | 5
**users**
id | username | password
2 | abc | cba
**clients**
id | company_name
5 | some_name
So the thing is that with acc_rel I have an account related with an user, a client, or whatever other table.
Because of reasons, not my reasons, I can't change this tables or the way they "works".
What I need to do is, retrieve the account information with the username or the company_name from the other tables, I need to do it in a single query so I can use a WHERE to filter, or ORDER BY and LIMIT. At least I think that I need it in a single query to do so.
This would be the perfect output:
account_id | account_owner
1 | abc
2 | some_name
So how can I do it? I don't know which tables I am going to do the JOIN or what column name I need, thought I can know it with PHP before doing the query.
A solution changing all the tables scheme would be appreciated too if the correct way to do this is another.
Based on #knowledge... answer I made this query and it works exactly like I need it.
SELECT AR.account_id,COALESCE(U.username, C.company_name) AS account_owner
FROM accounts AS A
INNER JOIN acc_rel AS AR ON A.id = AR.account_id
LEFT JOIN users AS U ON U.id = AR.target_id AND AR.target_table = 'users'
LEFT JOIN clients AS C ON C.id = AR.target_id AND AR.target_table = 'clients'
It is going to be a pain to maintain if I need to add new tables but well, it works the way I need.
select AR.account_id,COALESCE(U.username,C.company_name) as account_owner
from accounts A
join acc_rel AR on A.id=AR.account_id
left join users U on U.id=AR.traget_id and AR.target_table='user'
left join clients C on C.id=AR.traget_id and AR.target_table='clients'
So I am trying to deleting rows from multiple tables. I have looked at the following questions but none of them seem to solve my issue. No matter what I try I keep getting the following error:
Fatal error: Call to a member function bind_param() on a non-object
Here are some of the questions I have looked at to try help me before I asked a new question.
Delete with Join in MySQL
Deleting using LEFT JOIN
Deleting rows from multiple tables in MySQL
MySQL delete row from multiple tables
Basically what I'm trying to make is a function that allows the user to delete their account. Their account has information in 4 different tables and each table has a common 'id' but named differently.
Before I go on I should say that it might be the case that the user doesn't have any information stored in one of the tables. Don't know if this is why the error is thrown
Here is my table structure:
table users
user_id | firstname | surname
-------------------------------
10 | Joe | Bloggs
16 | Jane | Doe
table pets
pet_id | pet_user_id | pet_name
-------------------------------
1 | 10 | Rover
2 | 16 | Jess
table report
report_id | rep_user_id | rep_content
-------------------------------
1 | 10 | Hello World
2 | 16 | Goodbye World
table user_files
file_id | file_user_id| file
-------------------------------
1 | 10 | agreement.pdf
2 | 16 | cv.docx
So say I want to delete the user with the ID of 16 from each of these tables. The following are what I have tried so far.
First:
$stmt = $conn->prepare('DELETE * FROM users, pets, report, user_files WHERE user_id = ?, pet_user_id = ?, rep_user_id = ?, file_user_id = ? ');
Second:
$stmt = $conn->prepare('DELETE users.*, pets.*, reports.*, user_files.* FROM users LEFT JOIN pets ON pets.pet_user_id=users.user_id LEFT JOIN report ON report.rep_user_id=users.user_id LEFT JOIN user_files ON user_files.user_id=users.user_id WHERE user_id = ?');
Third:
$stmt = $conn->prepare('DELETE users, pets, report, user_files FROM
table1 as users
INNER JOIN table2 as pets on users.user_id = pets.pet_user_id
INNER JOIN table3 as report on users.user_id=report.rep_user_id
INNER JOIN table4 as user_files on users.user_id=user_files.user_id
WHERE users.user_id= ?');
The ? is bound using $stmt->bind_param('i', $user_id); where $user_id is being defined from a session variable.
I think your third statement should work, but I would built it with left joins.
Please try the following SQL Update:
DELETE users, pets, report, user_files
FROM users
LEFT JOIN pets ON (pets.pet_user_id=users.user_id)
LEFT JOIN report ON (report.rep_user_id=users.user_id)
LEFT JOIN user_files ON (user_files.file_user_id=users.user_id)
WHERE users.user_id=?;
You should try to execute this statement (with parameter "16" as "user_id") on your MySQL console and check if it works.
If not, I assume a problem with your binded variable $user_id. Can you provide us the output of a var_dump($user_id)?
I have a problem. I have 3 tables in mysql DB table1, table2 and table3. Im using PHP to retireve and write all information.
Table1 has list of organisations with IDs I have created for them:
Name of ogranisation | Organisation ID
--------------------------------------
org1 | 123
org2 | 234
org3 | 345
Table2 has list of services those organisations are using with their IDs and IDs of organisations from table1 that they belong to:
Name of service | Service ID | ID of organisation it belongs to
---------------------------------------------------------------
service1 | a1 | 123
service2 | b2 | 123
service3 | c5 | 345
Table3 has the list of users with IDs of services they provide from the table2:
Name of User | Service ID
-------------------------
user1 | a1
user2 | a1
user3 | c5
user4 | b2
Now I need, using those tables, to get number of users from table3 who provide services to organisations. Like if you look at table2 you can see that org1 with ID(123) is provided with service1 (a1) and service2 (b2) and in table3 you can see that there are 3 users providing services a1(2x) and b2 so there are 3 users providing services to org1.
How do I get that number form DB with PHP.
Please help and thank you
try this
SELECT COUNT(DISTINCT(user)) as cnt
FROM `table3` A
INNER JOIN `table2` B ON A.service_id = B.service_id
INNER JOIN `table1` C ON B.organization_id = C.organization_id
AND C.organization_id =123
see FIDDLE
To execute SQL use e.g. PDO, mysqli
the query looks like
SELECT
u.name
FROM user u
INNER JOIN service s
ON u.serviceId = s.serviceId
INNER JOIN ogranisation o
ON s.ogranisationId = o.ogranisationId
I have two tables, one table is called queuelist and the other is call info. In the queuelist table it just lists different IDs. I am trying to get the 'clientID' from that table and match it with the 'ID' in the other table that contains all of the info and display it back on the page. Here is how the tables look:
Table - queuelist
ID | clientID
-------------
1 | 589
2 | 254
3 | 486
Table - info
ID | Name | Phone
--------------------
256 | Bob | 5551231234
486 | Jack | 5551231234
589 | Jill | 5551231234
This is what they call joining tables, you should use a query like this:
SELECT i.ID, i.Name, i.Phone FROM `queuelist` AS q
LEFT JOIN `info` AS i ON (
q.clientID = i.ID
);
I'm using aliases for shorter notation in the above query (queuelist becomes q and info becomes i) and then set the join condition (the bit between the ON()) to be the clientID from the queuelist table should match the ID in the info table.
Also see http://dev.mysql.com/doc/refman/5.0/en/join.html for more details.
You need to use an inner join
select * from queuelist as ql inner join info as i on ql.clientID = i.ID
Though you might want to replace * with specific field names e.g
select ql.clientID, i.fieldname FROM....
Well, I see no difficulty in this using a JOIN.
SELECT * FROM queuelist JOIN info ON clientID = info.ID WHERE queuelist.ID = 2
"Where" would be another option.
SELECT Name, Phone FROM queuelist,info WHERE clientID = ID
Assuming you want only name and phone
I have 2 tables.
Table 1 : t_atc_list
id | a_name | s_title | r_name
------------------------------
80 | ss | 128 | 5
Where s_title & r_name is foreign key.
Table 2 : t_s_list
s_id | title
-------------
128 | Song Title
I want have used left join query on this..to select all values of 't_atc_list' if it mightbe in 't_s_list'..
$query=mysql_query("SELECT t.s_title, s.title from t_atc_list t LEFT OUTER JOIN t_s_list s ON t.s_title=s.s_id");
$row=mysql_fetch_array($query);
While if I use right join its working..
$query=mysql_query("SELECT t.s_title, s.title from t_s_list s RIGHT OUTER JOIN t_atc_list t ON t.s_title=s.s_id");
$row=mysql_fetch_array($query);
Whats the reason left join is not working but right join is going well? And I think both the queries are identical than whats the problem?
This is a strictly data related issue you have there, your SQL syntax is correct, but your data must not line up.
For a visualization of SQL, look here: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Also if you do a right join, that means you are simply getting data from the 2nd table, and nulls in the first. I am just guessing here as I see no real data examples.