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 ...
Related
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 encountering a problem I cannot bypass by myself and I that's why I am posting this question. There are a lot of other posts out there that give me half of the answer and I don't really know how to get it done.
I have 3 tables that contain informations about an ad. One table is "ad_names", another is "ad_locations" and the last one is "ad_details".
Ad_Names has : ad_title, ad_description, ad_date_added
Ad_Locations has : ad_country, ad_region, ad_city
Ad_Details has : ad_price, ad_author, ad_active
Basically I want apply location and details filters for an ad with a certain title.
For example, I want to search "food" keyword in ad-titles and then apply filters like "only from Kansas" or "Only from Kansas + price higher than 500USD". How do I do it?
first you will need to join the tables. do you know if you have a foreign key on the tables? like is there an Ad_ID field on all 3 tables? if the tables are large you may also want to index the fields you are searching. once you have all the data you will "filter" it with a where clause.
http://www.w3schools.com/sql/sql_where.asp
select * from ad_names as A
join ad_location as L on A.key=L.key
join ad_details as D on A.key=D.key
where A.ad_title like '%food%' and L.ad_region = 'Kansas' and D.ad_prics > 500;
depending on how your database is setup, you may get a cartesian result there, so you would have to look at your join. maybe a left outer join limiting the join in an on statement instead of in the where. it is hard to say without more information.
Join types
Assuming that Ad is a table with primary key id, which is a foreign key in each of the other tables (ad_id) then something along the lines of:
Location:
SELECT *
FROM Ad A
INNER JOIN Ad_Names N ON N.ad_id = A.id
INNER JOIN Ad_Locations L ON L.ad_id = A.id
INNER JOIN Ad_Details D ON D.ad_id = A.id
WHERE N.ad_title LIKE '%food%' AND L.ad_city = 'Kansas'
Price and location:
SELECT *
FROM Ad A
INNER JOIN Ad_Names N ON N.ad_id = A.id
INNER JOIN Ad_Locations L ON L.ad_id = A.id
INNER JOIN Ad_Details D ON D.ad_id = A.id
WHERE N.ad_title LIKE '%food%' AND L.ad_city = 'Kansas' AND D.ad_price > 500
I have a MYSQL query who have to list all post i want it to post. But it dont do it. It shows posts when i have more then one post in the table "meaOrder" with the same "ordCode". But when i have only on post in meaOrder, i don't show it. What can i do?
SELECT koden, wish, rnamn, bild, pris, cname, onsktext
FROM (
SELECT m.wishText as onsktext, m.meaOID as midn, m.ordcode as koden, w.wish as wish, r.meaName as rnamn, r.meaImg as bild,
r.meaPrice as pris, k.catName as cname from cats k, meals r, wishes w,
meaOrder m
join orders c on c.ordNR=4401
WHERE c.ordStatus=1 AND m.ordNR=c.ordNR AND m.meaID=r.meaID AND m.wishesID=w.id
AND r.catID=k.catID
) T
GROUP BY koden, rnamn, bild, pris, cname
ORDER BY midn DESC
TABLE orders
http://grab.by/m74E
TABLE meaOrder http://grab.by/m74Q
Try replacing the JOIN with RIGHT JOIN in this case. The difference is explained at JOIN Syntax page in MySQL docs . In short - JOIN returns row only if there are corresponding rows in both joined tables (inner join). LEFT JOIN / RIGHT JOIN return all rows from one of the tables and corresponding row if it exists from the other table (those are outer joins)
Do you need a subselect?
This seems to cover it:-
SELECT m.ordcode AS koden, w.wish AS wish, r.meaName AS rnamn, r.meaImg AS bild, r.meaPrice AS pris, k.catName AS cname, m.wishText AS onsktext
FROM cats k
INNER JOIN meals r ON r.catID = k.catID
INNER JOIN meaOrder m ON m.meaID = r.meaID
INNER JOIN wishes w ON m.wishesID = w.id
INNER JOIN orders c ON m.ordNR = c.ordNR
WHERE c.ordStatus = 1
AND c.ordNR = 4401
GROUP BY m.ordcode, r.meaName, r.meaImg, r.meaPrice, k.catName
ORDER BY midn DESC
I have the following bits of code:
$referrers_query =
select c.customers_id, c.customers_firstname, c.customers_lastname,
c.customers_email_address, c.customers_telephone, a.entry_street_address,
a.entry_city, a.entry_state, a.entry_country_id, n.countries_name,
a.entry_zone_id, a.entry_postcode, r.referrer_customers_id,
r.referrer_key, r.referrer_homepage, r.referrer_approved,
r.referrer_banned, r.referrer_commission from customers as c,
address_book as a, referrers as r, countries as n
where a.entry_country_id = n.countries_id and c.customers_id = r.referrer_customers_id
and a.address_book_id = c.customers_default_address_id order by c.customers_lastname;
What I would like to do, is instead of making a WHERE clause join, I'd like to nest the joins.
There are five database tables noted above.
customers, address_book, referrers, countries, zones.
But I have no idea where to get started. The main problem with this is using the above statement, I seem to lose a few records from the select. This is because some records are using a 'zone_id' = 0. A fix to this was to simply create a blank record for 0 but besides that, can I use a join to fix this?
The query written with explicit JOINs:
SELECT c.customers_id, c.customers_firstname, c.customers_lastname,
c.customers_email_address, c.customers_telephone, a.entry_street_address,
a.entry_city, a.entry_state, a.entry_country_id, n.countries_name,
a.entry_zone_id, a.entry_postcode, r.referrer_customers_id,
r.referrer_key, r.referrer_homepage, r.referrer_approved,
r.referrer_banned, r.referrer_commission
FROM customers AS c
JOIN referrers AS r ON (c.customers_id = r.referrer_customers_id)
JOIN address_book AS a ON (a.address_book_id = c.customers_default_address_id)
JOIN countries AS n ON (a.entry_country_id = n.countries_id)
ORDER BY c.customers_lastname
If you want to also get the info from the zones table if a match exists, you would need to add a LEFT JOIN like this:
SELECT c.customers_id, c.customers_firstname, c.customers_lastname,
c.customers_email_address, c.customers_telephone, a.entry_street_address,
a.entry_city, a.entry_state, a.entry_country_id, n.countries_name,
a.entry_zone_id, a.entry_postcode, r.referrer_customers_id,
r.referrer_key, r.referrer_homepage, r.referrer_approved,
r.referrer_banned, r.referrer_commission
FROM customers AS c
JOIN referrers AS r ON (c.customers_id = r.referrer_customers_id)
JOIN address_book AS a ON (a.address_book_id = c.customers_default_address_id)
JOIN countries AS n ON (a.entry_country_id = n.countries_id)
LEFT JOIN {zones table name} AS z ON (z.{zones id column name} = a.entry_zone_id)
ORDER BY c.customers_lastname
and also add the columns you want to select to the top of the query. A LEFT JOIN always returns a result from the left table (first one listed) and if there is no match in the right table, it returns NULLs for the right table's columns.
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);