Jump to content

join two table and get just one image path


streamland

Recommended Posts

The first picture is date table (add_table)

90b51bf273d7.jpg

second table (image_path_table) here I keep images path 

 

1554e9deb6f0.jpg

 

Please help built the query to select date from (add_table) and path to picture from  (image_path_table) 

$result = mysql_query("SELECT * FROM `add_new_car` AS a INNER JOIN `image_add_car` AS b ON a.id_add_car = b.user_id WHERE a.id_add_car =b.user_id and a.mark='$model' and a.model='$mark' and a.year='$year' ORDER BY a.id_add_car");

thank you 

SELECT * FROM `add_new_car` AS a INNER JOIN `image_add_car` AS b ON a.id_add_car = b.user_id WHERE (b.user_id, b.id_image_add_car) IN (SELECT user_id, MIN(id_image_add_car) FROM image_add_car GROUP BY user_id) AND a.mark='$model' and a.model='$mark' and a.year='$year' ORDER BY a.id_add_car

Thank you so much brand

I Could get something like this and will test it

Barand sorry but this query doesn't work it returs no result

SELECT * 
FROM `add_new_car` AS a 
INNER JOIN `image_add_car` AS b ON a.id_add_car = b.user_id 
WHERE a.mark='$mark' and a.model='$model' and a.year='$year' 
GROUP BY a.id_add_car

anyway the first query worked fine for me.

 

But I've got another question, what if some one will not select or select just a.mark='$mark' should I use

 

IF Statment to build a few query for example like

if empty($model)& empty($year)

SELECT * FROM `add_new_car` AS a INNER JOIN `image_add_car` AS b ON a.id_add_car = b.user_id WHERE (b.user_id, b.id_image_add_car) IN (SELECT user_id, MIN(id_image_add_car) FROM image_add_car GROUP BY user_id) AND a.mark='$mark'  ORDER BY a.id_add_car

Thank you!

Barand sorry but this query doesn't work it returs no result

Probably because you had $mark and $model reversed in your query.

 

 

 

If the user doesn't specify mark or model then leave them out of the WHERE clause EG

$where = array();
$whereclause = '';

if (!empty($mark)) {
    $where[] = "a.mark = '$mark'";
}
if (!empty($model)) {
    $where[] = "a.model = '$model'";
}
if (!empty($year)) {
    $where[] = "a.year = $year";
}
if (count($where > 0)) {
    $whereclause = 'WHERE ' . join(' AND ', $where);
}
$query = "SELECT * 
FROM `add_new_car` AS a 
INNER JOIN `image_add_car` AS b ON a.id_add_car = b.user_id 
$whereclause
GROUP BY a.id_add_car";

Probably because you had $mark and $model reversed in your query.

 

 

that was my carelessness the query from you worked fine thank you 

 

SELECT *

FROM `add_new_car` AS a

INNER JOIN `image_add_car` AS b ON a.id_add_car = b.user_id

WHERE a.mark='$mark' and a.model='$model' and a.year='$year'

GROUP BY a.id_add_car

 

now will check WHERE clause :-)

$where = array();
$whereclause = '';

if (!empty($mark)) {
    $where[] = "a.mark = '$model'";
}
if (!empty($model)) {
    $where[] = "a.model = '$mark'";
}
if (!empty($year)) {
    $where[] = "a.year = $year";
}
if (count($where > 0)) {
    $whereclause = join(' AND ', $where);
}
$result = mysql_query("SELECT * FROM `add_new_car` AS a INNER JOIN `image_add_car` AS b ON

                 a.id_add_car = b.user_id WHERE (b.user_id, b.id_image_add_car) IN (SELECT user_id, MIN(id_image_add_car)
				 
 FROM image_add_car GROUP BY user_id) AND $whereclause ORDER BY a.id_add_car");

Barand

Mark=toyota Model=Prado Year=2013

But why when I chose just Mark it doesn't gives any result but both together with Model=Prado it works fine

 

 

$where[] = "a.mark = '$model'";

 

 

$where[] = "a.model = '$mark'";

 

Is it a deliberate obfuscation ploy of yours to to put the mark value in $model and the model value in $mark?

 

All your queries seem to have it like that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.