I am working on a PHP file and MySQL.
On a file, I need to select records from three tables.
If a make a query with two tables:
$query_Recordset1 = "
SELECT * FROM tbgastos
LEFT JOIN tbconceptosgastos
ON tbgastos.tipoGasto = tbconceptosgastos.idConceptoGasto
LEFT JOIN tbobras
ON tbgastos.obra = tbobras.idObra
ORDER BY fecha DESC
";
it works fine, but if I try to make it with three tables:
$query_Recordset1 = "
SELECT * FROM tbgastos
LEFT JOIN tbconceptosgastos
ON tbgastos.tipoGasto = tbconceptosgastos.idConceptoGasto
LEFT JOIN tbobras
ON tbgastos.obra = tbobras.idObra
LEFT JOIN tbproveedores
ON tbgastos.proveedor = tbproveedores.nombreProveedor
ORDER BY fecha DESC
";
the third table (tbproveedores) records are not shown.
What am I doing wrong?
UPDATED
tbgastos
tbproveedores
In your table tbgastos, you have a foreign key of a int type (proveedor).
And you want it to match to the table tbproveedores. Don't you want to point on tbproveedores.idProveedor ?
$query_Recordset1 = "
SELECT * FROM tbgastos
LEFT JOIN tbconceptosgastos
ON tbgastos.tipoGasto = tbconceptosgastos.idConceptoGasto
LEFT JOIN tbobras
ON tbgastos.obra = tbobras.idObra
LEFT JOIN tbproveedores
ON tbgastos.proveedor = tbproveedores.idProveedor
ORDER BY fecha DESC
";
Related
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 PHP website with a table that prints out search results from a MySQL database via form with an input box. I use this SQL syntax and it works fine. I can search in one filed to get results from location.room_name and users.user_name:
SELECT:
SELECT computers.*, location.room_name AS location
FROM computers
LEFT JOIN location ON computers.location = location.id
LEFT JOIN users ON computers.user_1 = users.id".
" ".$searchCriterias."
ORDER BY id
WHERE:
$searchCriterias =
"WHERE
location.room_name LIKE '%".$s."%' OR
users.user_name LIKE '%".$s."%'
";
$s is the string from the search form.
The thing is that I have computers.user_1, computers.user_2 and computers.user_3 that all corresponds to users.id as in the syntax above. When I search for a username I would like to match it with user_1, user_2 and user_3 but I don't get it to work. I can only match it with one (in this case user_1).
I have tried to add LEFT JOIN users ON computers.user_2 = users.id and gets this error message: Not unique table/alias: 'users'
and...
LEFT JOIN users AS u1 ON computers.user_1 = users.id for all three and gets this messge: Unknown column 'users.id' in 'on clause'.
Why doesn't it work?
As you said you have 3 columns in computers table then try following while join. Also added GROUP BY to get unique result per user.
$query = "SELECT computers.*, location.room_name AS location
FROM computers
LEFT JOIN location ON computers.location = location.id
LEFT JOIN users ON (computers.user_1 = users.id
OR computers.user_2 = users.id
OR computers.user_3 = users.id ) ".
$searchCriterias . " GROUP BY users.id ORDER BY id";
Change your ON condition to IN statement :
SELECT computers.*, location.room_name AS location
FROM computers
LEFT JOIN location ON computers.location = location.id
LEFT JOIN users
ON ((users.id = computers.user_1) or
(users.id =computer.user_2 and not exists(select 1 from users t where t.id = computers.user_1)) OR
(users.id = computer.user_3 AND not exists(select 1 from users p where p.id IN(computer.user_2,computer.user_1))))".
" ".$searchCriterias."
ORDER BY id
Another option is to join against users 3 times, and amend your WHERE clause to check against 3 different tables.
Might need a DISTINCT to remove duplicates as I presume names can match multiple times.
This has the advantage over using an OR in the ON clause that it can still use indexes for the joins.
SELECT computers.*, location.room_name AS location
FROM computers
LEFT JOIN location ON computers.location = location.id
LEFT JOIN users users1 ON computers.user_1 = users1.id
LEFT JOIN users users2 ON computers.user_2 = users2.id
LEFT JOIN users users3 ON computers.user_3 = users3.id".
" ".$searchCriterias."
ORDER BY id
And the WHERE clause
$searchCriterias =
"WHERE
location.room_name LIKE '%".$s."%' OR
users1.user_name LIKE '%".$s."%' OR
users2.user_name LIKE '%".$s."%' OR
users3.user_name LIKE '%".$s."%'
";
want to combine this 4 query in single mysql query and my current query is
$sql1 = "SELECT * FROM projectFiles pf
INNER JOIN projectFileFolders pff ON pf.folderID = pff.folderID
WHERE pff.projectID = '".$row['projectID']."' AND folderType=3"
$sql2 = "SELECT * FROM projectFiles pf
INNER JOIN projectProducts pp ON pf.fileID = pp.fileID
WHERE pp.projectID = '".$row['projectID']."'"
$sql3 = "SELECT * FROM projectFiles pf
INNER JOIN projectMaintenance pm ON pf.fileID = pm.fileID
WHERE pm.projectID = '".$row['projectID']."' "
$sql4 = "SELECT * FROM projectFiles pf
INNER JOIN projectServices ps ON pf.fileID = ps.fileID
WHERE ps.projectID = '".$row['projectID']."' "
If I understand your requirement correctly, you have four tables, and you want to include any records from those tables whose projectID matches the one in your provided $row['projectID'] field. If so, you can simply combine all your queries like this:
$sqlWhatever =
"SELECT * FROM projectFiles pf
INNER JOIN projectFileFolders pff ON pf.folderID = pff.folderID
INNER JOIN projectProducts pp ON pf.fileID = pp.fileID
INNER JOIN projectMaintenance pm ON pf.fileID = pm.fileID
INNER JOIN projectServices ps ON pf.fileID = ps.fileID
WHERE pff.projectID = '".$row['projectID']."' AND folderType=3
OR pp.projectID = '".$row['projectID']."'
OR pm.projectID = '".$row['projectID']."'
OR ps.projectID = '".$row['projectID']."' "
Now, you haven't said whether each of the four tables has the same fileID's or not. If they do not, then change your INNER join to a LEFT join, or you'll only get records that have a common fileID in each of the four tables.
I have a database containing two tables:
products_attributes ( id , image )
options_values ( id , text )
The id value is the same in both tables. Now I need to output the text and the image based on the same id from both tables.
Then possibly base this on the loop. I have created something like this, but it doesn't work correctly.
$sqlquery = "SELECT * FROM products_attributes JOIN options_values WHERE BY
id=id ASC LIMIT 0,40";
$sqlresult = #mysql_query($sqlquery);
while ($content = mysql_fetch_array($sqlresult, MYSQL_NUM)) {
echo "<img src='$content[1]'/> $content[2]";
}
#EDIT
So the query ended up like this, however I can not get the attributes_image (table column name) to display. It does however work using the numerical system.
$sqlquery = "SELECT * FROM zen_products_attributes pa JOIN zen_products_options_values ov ON pa.options_values_id = ov.products_options_values_id LIMIT 0,40;";
$sqlresult = #mysql_query ($sqlquery);
while ($content = mysql_fetch_array($sqlresult, MYSQL_NUM)){
echo $content['attributes_image'];
;}
Use
$sqlquery = "SELECT * FROM products_attributes JOIN options_values
ON products_attributes.id=options_values.id ASC LIMIT 0,40";
1:You need to use alias for tables when you are joining on same column
name to avoid ambiguity.i.e 'id=id' is wrong.
2.Syntax error.there is no 'WHERE BY' in mysql.
$sqlquery = "SELECT * FROM
products_attributes pa
JOIN options_values ov
on pa.id=ov.id ASC LIMIT 0,40";
Edit answer:
while ($content = mysql_fetch_array($sqlresult, MYSQL_NUM)){
echo $content['attributes_image'];
;}
--^(extra semi colon,remove this)
if you have fields with same name give them alias.
SELECT p.`desc` as description, v.`desc` FROM products_attributes as p
JOIN options_values as v ON p.id=v.id
ASC LIMIT 0,40
SELECT
*
FROM
products_attributes pa
JOIN
options_values ov
ON
pa.id = ov.id
LIMIT
0,40;
SELECT * FROM products_attributes AS pa JOIN options_values AS ov ON pa.id=ov.id ORDER BY ov.id ASC LIMIT 0,40
I am trying to come up with a query where a 3rd table will join another two. The problem is there are different versions of this first table and I need the query to look at a column in table 1 and then choose the correct table to join but the resulting query must return all rows in table 1 just join an extra table 3 to specific rows which meet the WHERe clause. Is this even possible?
Example:
Table 1 = User
Table 2 = Banners
Table 3 = UserSettings OR AdminSettings OR Nothing
Query would say...
WHERE User.rank='admin' LEFT JOIN AdminSettings
WHERE User.rank='user' LEFT JOIN UserSettings
My current query is this:
$query = "SELECT * FROM Users as U WHERE U.user_id = :id ";
$query .= "LEFT JOIN Banners as B ON U.banner_id = B.banner_id ";
$query .= "LEFT JOIN AdminSettings ON U.server_id = AdminSettings.user_id WHERE U.rank='Admin' ";
$query .= "LIMIT 1";
Problem is that it only shows rows that have User.rank='admin' and does not show the rows where User.rank='user' rest.
Join table3 twice and use CASE within the select statement
SELECT a.*,
b.*
CASE WHEN a.rank = 'admin'
THEN c.user_ID -- gets value from AdminSettings
ELSE d.user_ID -- gets value from UserSettings
END as ColName1,
CASE WHEN a.rank = 'admin'
THEN c.colName -- gets value from AdminSettings
ELSE d.colName -- gets value from UserSettings
END as ColName2
FROM users a
INNER JOIN banners b
ON a.banner_id = b.banner_id
LEFT JOIN AdminSettings c
ON a.server_ID = c.user_ID
LEFT JOIN UserSettings d
ON a.server_ID = d.user_ID
Try using a IF clause in the fields list, like this:
$query = "SELECT field1, field2, field3, IF(User.rank = 'admin', A.field, B.field)";
$query .= " FROM Users as U";
$query .= " LEFT JOIN Banners as B ON U.banner_id = B.banner_id";
$query .= " LEFT JOIN AdminSettings as A ON U.server_id = A.user_id";
$query .= " LIMIT 1";
Something like that should do the trick.