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 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Edited by streamland
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

$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

Edited by streamland
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.