I'm trying to replace the value of "transfers.pickup_areas_group_id" and "transfers.drop_areas_group_id" with the values from the table "areas_group", using IDs
I'm using this query:
SELECT
transfers.id AS transfer_id,
transfers.name AS transfer_name,
transfers.pickup_areas_group_id AS transfer_pickup_areas_group_id,
transfers.drop_areas_group_id AS transfer_drop_areas_group_id,
transfers_pricing.vehicle_id AS vehicle_id,
transfers_pricing.date_start AS date_start,
transfers_pricing.date_end AS date_end,
transfers_pricing.price AS price
FROM transfers
INNER JOIN transfers_pricing ON transfers_pricing.transfer_id = transfers.id
I tried an extra INNER JOIN to replace the first value "transfers.pickup_areas_group_id", but I couldn't find a way to replace the second one "transfers.drop_areas_group_id"
I tried this query:
SELECT
transfers.id AS transfer_id,
transfers.name AS transfer_name,
transfers.pickup_areas_group_id AS transfer_pickup_areas_group_id,
areas_group.area_id AS pickup_area_ids,
transfers.drop_areas_group_id AS transfer_drop_areas_group_id,
transfers_pricing.vehicle_id AS vehicle_id,
transfers_pricing.date_start AS date_start,
transfers_pricing.date_end AS date_end,
transfers_pricing.price AS price
FROM transfers
INNER JOIN transfers_pricing ON transfers_pricing.transfer_id = transfers.id
INNER JOIN areas_group ON areas_group.id = transfers.pickup_areas_group_id
Thank you,
Basically, you need another join on areas_group; to disambiguate the two references to the same table, you need to use a table alias.
Actually, it is a good practice to use table aliases for all tables that come into play in the query: this makes the query shorter to read and write.
SELECT
t.id AS transfer_id,
t.name AS transfer_name,
t.pickup_areas_group_id AS transfer_pickup_areas_group_id,
ag1.area_id AS pickup_area_ids,
t.drop_areas_group_id AS transfer_drop_areas_group_id,
ag2.area_id AS drop_area_ids
tp.vehicle_id AS vehicle_id,
tp.date_start AS date_start,
tp.date_end AS date_end,
tp.price AS price
FROM transfers t
INNER JOIN transfers_pricing tp ON tp.transfer_id = t.id
INNER JOIN areas_group ag1 ON ag1.id = t.pickup_areas_group_id
INNER JOIN areas_group ag2 ON ag2.id = t.drop_areas_group_id
I am trying to write a query with multiple OR and using DISTINICT for multiple column.But the result is NULL what i am missing in my query. Anyone can help me here please.
SELECT DISTINCT
P.user_p_id,
P.surl,
M.user_id,
M.user_p_id
FROM users P, page M
WHERE (P.user_p_id = '$urlid' OR P.surl = '$urlid' OR M.user_id = '$urlid' OR M.user_p_id = '$urlid')
You should do a proper join with an ON clause:
SELECT DISTINCT
P.user_p_id,
P.surl,
M.user_id,
M.user_p_id
FROM users P INNER JOIN page M
ON M.user_id = P.user_p_id
WHERE '$urlid' IN (P.user_p_id, P.surl, M.user_id, M.user_p_id)
I use ON M.user_id = P.user_p_id although it's not clear if you want these columns to be matched.
You may change the type of the JOIN to LEFT if this is what you need.
I had a SELECT query with a LEFT JOIN working as desired. I then added one more table via a smilar LEFT JOIN and now I am getting a wierd result. Basically, for a group_concat where I was getting one item for every record, I am getting eight records. I don't see why this is happening because the join to the new table is analagous to several other joins that do not have this problem (that I have omitted from the example for clarity).
Here is the query that is fine:
$sql = "SELECT t.*,
group_concat(tf.todoid) as `tftodoid`,
group_concat(tf.id) as `tfid`,
group_concat(tf.filedescript) as `tffiledescript`,
group_concat(tf.filename) as `tffilename`,
group_concat(tf.founderid) as `tffounderid`,
group_concat(tf.ext) as `tfext`,
group_concat(tf.lasttouched) as `tilt`
FROM titles `t`
LEFT JOIN titlefiles `tf`
ON (tf.todoid = t.id AND tf.founderid = '$userid')
WHERE t.userid='$userid'
GROUP BY t.id";
And here is the query with the extra join that is now spilling out the multiple copies of the items:
$sql = "SELECT t.*,
group_concat(tf.todoid) as `tftodoid`,
group_concat(tf.id) as `tfid`,
group_concat(tf.filedescript) as `tffiledescript`,
group_concat(tf.filename) as `tffilename`,
group_concat(tf.founderid) as `tffounderid`,
group_concat(tf.ext) as `tfext`,
group_concat(tf.lasttouched) as `tilt`,
group_concat(s.id) as `stepid`,
group_concat(s.step) as `steps`
FROM titles `t`
LEFT JOIN titlefiles `tf`
ON (tf.titleid = t.id AND tf.founderid = '$userid')
LEFT JOIN steps `s`
ON s.titleid = t.id
WHERE t.userid='$userid'
GROUP BY t.id";
Here is an example of output in JSON showing the difference:
First query:
"tfid":"56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81"
Second query:
"tfid":"56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81,56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81,56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81,56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81,56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81,56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81,56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81,56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81,56,57,58,59,60,61,62,63,64,65,66,67,68,75,76,81",
I suspect the problem has something to do with the JOIN or with the Group By statements but I can't see how to fix.
How can I ensure that I get only one fileid per file as opposed to eight?
Alter the line as follows:
group_concat(DISTINCT tf.id) as `tfid`,
This then only gets you the unique ids.
If you want them ordered add:
group_concat(DISTINCT tf.id ORDER BY tf.id ASC) as `tfid`,
I am using the Sql query below:
$query = mysql_query("SELECT * FROM `venues`
LEFT JOIN `follows` USING (venue_id)
LEFT JOIN `stats` USING (venue_id, user_id)
WHERE follows.user_id = $userid");
The problem is that it's not showing some fields in the stats table.
So what I would thinking the problem might be (might be wrong), is that I need to select all the fields of that table?
If this is the case, is there a way of telling it to select * the fields for the 3 tables?
For example:
SELECT * FROM `venues`, SELECT * FROM `follows`, SELECT * FROM `stats` LEFT JOIN ....
use alias for each table then
select A., B., C.* from venues as A left join Follows as B.....
You can use alias for tables. Here's your request using those aliases:
$query = mysql_query("SELECT a.*, b.*, c.* FROM `venues` as b
LEFT JOIN `follows` as b USING (venue_id)
LEFT JOIN `stats` as c USING (venue_id, user_id)
WHERE follows.user_id = $userid");