MySQL Output different values by the same id - php

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

Related

Trying my first sql left join - isn't working

HiI'm trying to join two database table and extract the rows to php variables. But it's my first join function, and I'm not getting any values returned to my form.
SELECT s.supplierid,
s.name,
s.address,
s.zipCode,
s.cityName,
s.region,
s.country,
s.phone,
s.fax,
s.contactMame,
s.contactTitle,
s.contactPhone,
s.contactEmail,
s.notes,
s.employeeid,
e.employeeid as emp_id,
e.name as emp_name
FROM suppliers s
LEFT JOIN employees e
ON s.employeeid=e.employeeid
WHERE supplierid=? limit 0,1
ORDER BY name DESC
LIMIT :from_record_num, :records_per_page";
And then I assign them to variables like this
$name = $row['name'];
$address = $row['address'];
$emp_name = $row['emp_name'];
I need to use it another time in my code, but to extract all rows from the table. Is there a way to use
SELECT *
in a left join?
Try something like this:-
( assumes pagination vars $from_record_num and $records_per_page )
$sql="select
s.supplierid,
s.name,
s.address,
s.zipcode,
s.cityname,
s.region,
s.country,
s.phone,
s.fax,
s.contactmame,
s.contacttitle,
s.contactphone,
s.contactemail,
s.notes,
s.employeeid,
e.employeeid as 'emp_id',
e.name as 'emp_name'
from suppliers s
left join employees e on s.employeeid=e.employeeid
where s.supplierid=?
order by s.name desc
limit {$from_record_num}, {$records_per_page}";

Select with inner join

i need select some data from two tables ,
please help me use inner join for this selection .
players in selction2 must not be in selection1...
first select :
$rs = "SELECT *
FROM `player`
WHERE `status`=1 AND `credit`>=1 AND `username` NOT LIKE '$user'
ORDER BY ls ASC,credit DESC
LIMIT 0 ,10;
Second: this players must remove from result of selection1
$rs2 = "SELECT *
FROM `ip_log`
WHERE `playerid`='$ui' AND `win`='1' AND `date`='$date' ";`
You can use LEFT JOIN for this:
This shows the log messages for everyone not in selection 1.
SELECT l.*
FROM ip_log AS l
LEFT JOIN
(SELECT username
FROM player
WHERE status = 1 AND credit >= 1 AND username NOT LIKE '$user'
ORDER BY ls ASC, credit DESC
LIMIT 10) AS p
ON l.player = p.username
WHERE win = 1 and date = '$date'
AND p.username IS NULL
This shows the top 10 player data, except the ones with log messages in selection 2
SELECT p.*
FROM player AS p
LEFT JOIN ip_log AS l ON l.player = p.username AND l.win = 1 AND l.date = '$date'
WHERE p.status = 1 AND p.credit >= 1 AND p.username NOT LIKE '$user'
AND l.player IS NULL
ORDER BY p.ls ASC, p.credit DESC
LIMIT 10
In both cases, testing a column in the second table with IS NULL makes it return only the rows in the first table that don't have a match in the second table. See
Return row only if value doesn't exist
You can do it with LEFT JOIN
SELECT player.*,ip_log.* FROM `player` LEFT JOIN `ip_log` ON player.id!=ip_log.playerid GROUP BY player.id

Php and MysQL JOIN tree table

I have one question about JOIN from PHP section .
I know this question have in stackoverflow.com but that solution is not helped me.
I am using this code for join two table:
<?php
$select_posts = "SELECT users.uid,users.user_name,user_uploads.uid_fk,user_uploads.image_path
FROM users
JOIN user_uploads
ON users.uid = user_uploads.uid_fk
WHERE user_name='$user_name' order by rand() LIMIT 0,4";
$run_posts = mysql_query($select_posts);
while($row=mysql_fetch_array($run_posts)) {
$image_path=$row['image_path'];
$uid_fk = $row['uid_fk'];
?>
This code will work but i want to add another table here.
other table name is: message
table inside have:
msg_id
uid_fk
comment_count
like_count
How can I add a third table here? Anyone can help me!
Just join the third table with an another JOIN keyword in your query.
SELECT table_users.uid,table_users.user_name,table_user_uploads.uid_fk,table_user_uploads.image_path, table_message.msg_id
FROM table_users
JOIN table_user_uploads
ON table_users.uid = table_user_uploads.uid_fk
JOIN table_message
ON table_message.uid_fk = table_users.uid
WHERE user_name='$user_name' order by rand() LIMIT 0,4

MySQL LEFT JOIN example

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
";

how can i arrange the output of my data i am using group concat?

i want to arrange the output of my data in descending Here is my code:
$result3 = mysql_query("SELECT grade1.Semester, curriculum.SCode, curriculum.SDesc, curriculum.Lec, curriculum.Lab, curriculum.Units, curriculum.Prereq, GROUP_CONCAT(grade1.Grade1) as Grade1 , students.StudNo, grade1.YearLevel
FROM students
INNER JOIN grade1
ON students.StudNo = grade1.G1StudNo
INNER JOIN curriculum
ON curriculum.SCode = grade1.G1SCode
WHERE StudNo = '$id'
GROUP BY StudNo,SCode ")
Here is the output:
.
What i want to happen is 5,5,1.. how can i do that when i am using group_concat?
Check out the docs on GROUP_CONCAT:
GROUP_CONCAT(grade1.Grade1 ORDER BY grade1.Grade1 DESC)

Categories