I have table A, table B, and table C. Using a value from table A, I want to select things from table B and table C.
So I wrote this:
'SELECT * FROM a LEFT JOIN b ON a.w = b.x LEFT JOIN c ON a.y = c.z';
The problem is I have colliding column names in the three tables, and the last table column names overwrite the second one.
How can I fix this, I tried using the AS keyword but couldn't make it work, such as:
'SELECT * FROM a LEFT JOIN b AS bb ON a.w = bb.x LEFT JOIN c AS cc ON a.y = cc.z';
but it doesn't work. Maybe LEFT JOIN is not the best option? What I need to get is this:
a_id: u
a_name: v
b_id: w
b_name: x
c_id: y
c_name: z
As you can see, the array/object has a prefix for each table (table name + _) so there is no collision.
I'm hope I'm being clear, let me know if I'm not so I can edit the post. Thanks.
You need to provide aliases for each of the column names individually:
SELECT a.id AS a_id, a.name AS a_name,
b.id AS b_id, b.name AS b_name,
c.id AS c_id, c.name AS c_name
FROM a
LEFT JOIN b ON a.w = b.x
LEFT JOIN c ON a.y = c.z
Related
I have to get A.Ref_id, B.Ref_id and B_Id's Ref_id
Table A_B having Column_A_ID and Column_B_ID
Table A having ID, Ref_id, Name, B_Id (This is the B.ID from table B)
Table B having ID, Ref_id, Name
Currently I'm having the following query
SELECT A.Ref_id as A_Ref_Id, B.Ref_id as B_Ref_Id, B_Id
FROM A_B
JOIN A on A_B.Column_A_ID = A.Id
JOIN B on A_B.Column_B_ID = B.Id
JOIN B AS Main_B on B.id = A.B_id;
By this query I'm getting the A.Ref_Id and B.Ref_Id columns correctly as they are showing their relevant Ref_id but for B_Id I want to have the Ref_id and it is showing the B.id instead.
You want this (right?): The value of B.Ref_id in the row where B.ID = A.B_Id while this row in turn must have A.ID=A_B.Column_A_ID. And the whole thing for every row in A_B:
SELECT A.Ref_id as A_Ref_id, B.Ref_id as B_Ref_id, Main_B.Ref_id
FROM A_B
JOIN A ON A_B.Column_A_ID = A.Id
JOIN B ON A_B.Column_B_ID = B.Id
JOIN B AS Main_B on Main_B.id = A.B_id;
(Last line of code with the important correction which was proposed by #Mojtaba)
So far, I see the ON condition of the last join has to use the alias name of table B:
SELECT A.Ref_id as A_Ref_Id, B.Ref_id as B_Ref_Id, B_Id
FROM A_B
JOIN A on A_B.Column_A_ID = A.Id
JOIN B on A_B.Column_B_ID = B.Id
JOIN B AS Main_B on Main_B.id = A.B_id;
By the way, having some example in sqlFiddle could be more helpful
I have one issue. I need to retrieve data from database as per some condition but some cases it fails. I am explaining my query below.
select b.member_id as b_member_id,
b.rest_name,
b.city,
b.proviance,
b.postal,
b.address,
b.country,
b.person,
b.mobile,
b.url,
b.status,
b.premium,
b.image,
b.business_phone_no,
b.email,
b.multiple_image,
b.latitude,
b.longitude,
b.quadrant,
d.member_id as d_member_id,
d.day_id,
d.cat_id,
d.subcat_id,
d.comment,
d.city,
d.special_images,
c.cat_id,
c.special,
sub.subcat_id,
sub.subcat_name,
sub.status,
sl.day_id,
sl.member_id,
sl.date_from,
sl.date_to
from db_restaurant_basic as b left join db_restaurant_detail as d on b.member_id=d.member_id
left join db_category as c on d.cat_id=c.cat_id
left join db_subcategory as sub on d.subcat_id=sub.subcat_id
left join db_special_images as sl on d.day_id=sl.day_id
where b.city='2' and d.day_id='4' and c.special='1'
and (((sl.date_from IS NULL or sl.date_from='') and (sl.date_to IS NULL or sl.date_to='')) or( sl.date_from <='2016-10-27' and sl.date_to >= '2016-10-27' ))
and b.status=1
and sub.status=1 group by d.subcat_id ORDER BY b_member_id DESC
Here my problem is some value also is coming which does not match the condition. Here b.city='2' but some value is coming which city=0 only also. Here i need the value should come as per proper matching. Please help me.
You are selecting d.city - from the db_restaurant_basic table - but the condition you set is b.city='2' - on the db_restaurant_detail table.
So any results with a city of 0, will show the city from the d / db_restaurant_detail table.
If you need to filter on that as well, you need to add and d.city=2.
You should probably check if you can normalize your database structure more to avoid having the same data in different tables.
Using the answer window's formatting options...
WHERE b.city = 2
AND d.day_id = 4 -- NOTE THAT THIS IS AN INNER JOIN!
AND c.special = 1 -- AND SO IS THIS !!
Since you have city twice in the selection list (b.city and d.city) I have to assume the city='0' value you mention is really d.city='0'. To make sure that also d.city is '2' you can add an additional condition in the where clause or you specify the join like this
select ... from db_restaurant_basic as b left join db_restaurant_detail as d on d.member_id=d.member_id and b.city = d.city left join ...
or even like this, getting rid of the ambiguity
select ... from db_restaurant_basic as b left join db_restaurant_detail as d using(member_id, city) left join ...
I have a SQL query that returns an array like this:
nr|id |reference
#1|"1311"|"0"
#2|"1731"|"1260"
#3|"1332"|"1261"
#4|"1312"|"1311"
#5|"1316"|"1312"
#6|"1261"|"1316"
#7|"1260"|"1332"
now the problem is that the 2nd column and the 3rd column represent the order of the items, so the correct order of the above array would be
1 - 4 - 5 - 6 - 3 - 7 - 2
because the 3rd column tells what the id is after which the current item follows.
is there any way to put this into an SQL Query? A solution to sort the array afterwards with PHP would be acceptable, too.
Note that MySQL does not support recursion, so you have to invent it in some way or (better) rearrange your problem so that it doesn't require it. Anyway, just for fun, here's a solution of sorts... (NOTE: I've used NULL to represent orphans)
SELECT *, FIND_IN_SET(nr,(
SELECT CONCAT_WS(',',a.nr,b.nr,c.nr,d.nr,e.nr,f.nr,g.nr)
FROM my_table a
LEFT
JOIN my_table b
ON b.reference = a.id
LEFT
JOIN my_table c
ON c.reference = b.id
LEFT
JOIN my_table d
ON d.reference = c.id
LEFT
JOIN my_table e
ON e.reference = d.id
LEFT
JOIN my_table f
ON f.reference = e.id
LEFT
JOIN my_table g
ON g.reference = f.id
LEFT
JOIN my_table h
ON h.reference = g.id
WHERE a.reference IS NULL
)) a FROM my_table ORDER BY a;
http://www.sqlfiddle.com/#!2/347578/1
I am trying to unify a pair of queries with a LEFT JOIN, so that I can perform an ORDER BY on a desired column.
Table A has an exclusive one-to-many relationship with Table B, and table B has an exclusive one-to-many relationship with Table C.
I want to get the maximum value of a column in Table C, for each row in Table A.
what I used to have was:
$tableA = SELECT * FROM tableA;
for each row in $tableA {
$maxValue = SELECT MAX(value) FROM tableC WHERE tableB_id IN (SELECT tableB_id FROM tableB WHERE tableA_id={$row['tableA_id']}) GROUP BY tableB_id;
}
and now I'm thinking along the lines of:
SELECT * FROM tableA LEFT JOIN (SELECT tableA_id, MAX(max_c_value) FROM (SELECT tableB_id, MAX(value) max_c_value FROM tableC GROUP BY tableB_id) t GROUP BY tableA_id) USING(tableA_id)
but I know that's gibberish. I am not sure where to go from here.
I'm finding it hard to explain the problem, sorry.
SELECT A.ID, MAX(C.MyField) as CField
FROM A
LEFT OUTER JOIN B
ON A.ID = B.A_ID
LEFT OUTER JOIN C
ON B.ID = C.B_ID
GROUP BY A.ID
You will get null value for CField if there is no rows corresponding.
select max(table3.field),table1.field from table1
join table2 on table1.id=table2.table1_id
join table3 on table2.id = table3.table2_id
group by table1.field
You were closer on the first try I think. You want something like this:
SELECT MAX(tableC.cValue) FROM tableA
LEFT JOIN tableB
ON tableA.tableA_id = tableB.tableA_id
LEFT JOIN tableC
ON tableB.tableB_id = tableC.tableB_id
http://www.w3schools.com/sql/sql_join.asp
I have managed to solve it on my own, only to come back here and find I was making things far too complicated for myself, getting lost in a sea of tables!
This code did actually work, but I have already replaced it with the simpler code suggested above:
SELECT * FROM A LEFT JOIN (
SELECT A_ID, MAX(val) FROM (
SELECT * FROM B LEFT JOIN (
SELECT B_ID, MAX(val) val FROM C GROUP BY B_ID
) x USING(B_ID)
) y GROUP BY A_ID
) z USING(A_ID);
I feel like I'm writing a word problem, but it's really puzzling me and I really hope someone here can solve it:
I want to select one row from table A. Table A includes the attributes Name and Number. But before I finish the query, I want to check it against table B. Table B includes Name, Number, and the Username of the user.
Based on the users' input, it inserts rows into table B that include their Username along with a Name and Number.
Now in my query where I select a row from table A, I want to make sure that there are no rows in table B with matching Name and Number for that particular User.
I have tried WHERE (A.Name = B.Name AND A.Number = B.Number AND B.Username != '$username') but I think I was way off base with that.
Any help would be... amazing.
SELECT
A.id
FROM
A
LEFT OUTER JOIN B ON
(A.Name = B.Name AND A.Number = B.Number)
WHERE
B.Name IS NULL
AND B.Number IS NULL
AND B.Username = ?
select a.id
from a
where
a.name=:name
and
not exists(select 1 from b where b.id=a.id and b.name=a.name)
IF NOT EXISTS ( SELECT 1
FROM tableA a
INNER JOIN tableB b
ON a.name = b.name
AND a.number= b.number
AND b.UserName = 'user' and b.name = 'name'and b.number = 'number')
SELECT *
FROM tableA x
WHERE x.name = 'name'and x.number = 'number'