What I'm looking to generate is the wp_tylerposts table as it is, with the same specifications as the query (selecting only those with post_type='sponsor', but I'd like to reference the other two tables (object_id from wp_tylerterm_relationships and the corresponding name from wp_tylerterms) so that added on to wp_tylerpostswould be the corresponding name value from wp_tylerterms (added to the table would be "Gold Sponsors", for example).
Hopefully that makes sense. I'm sure there's a pretty simple solution, and I've tried my hand at some join queries without any luck... haven't done any of the stuff in a long time. Any help would be much appreciated!
Edit: I've come closer, I think, but I still can't retrieve the "name" column value from wp_tylerterms, here's what I have:
SELECT
c.ID, c.post_title, a.name
FROM
wp_tylerposts c
LEFT JOIN
wp_tylerterm_relationships b
ON
c.ID = b.object_id
LEFT JOIN
wp_tylerterms a
ON
b.object_id = a.term_id
WHERE
c.post_type = 'sponsor'
In second join, you use b.object_id instead of b.term_taxonomy_id. Your query should look like this:
SELECT
c.ID, c.post_title, a.name
FROM
wp_tylerposts c
LEFT JOIN
wp_tylerterm_relationships b
ON
c.ID = b.object_id
LEFT JOIN
wp_tylerterms a
ON
b.term_taxonomy_id = a.term_id
WHERE
c.post_type = 'sponsor'
Related
I'm having some trouble trying to get all the data out of this database in one query.
Here is the Database scheme
I know that there is a way to do this with a join but i'm not sure how.
I need all the data from Questionnaire, Booking, and Customers.
I think i have to join on the Booking has customers but i'm not sure.
At the moment i have something like this
SELECT *
FROM Booking,Questionaires,Customers
WHERE accepted = 0
JOIN ON Booking_idBooking = Customers_idCustomers
Can anyone help me out?
Thanks beforehand!
You could use inner join this way
SELECT * FROM Booking_idBooking as BB
INNER JOIN Booking as B on BB.Booking_idBooking = B.idBooking
INNER JOIN Customers as C on BB.Customers_idCustomers = C.idCustomers
INNER JOIN Questionaires as Q on Q.idQuestionaires = B.uestionaires_idQuestionaires
WHERE B.accepted = 0 ;
I would go about doing something like this, but you may need left joins depending one your circumstances.
select
*
from
customers c
inner join booking_has_customers bhs on
bhs.Customers_idCustomers = c.idCustomers
inner join booking b on
b.idBooking = booking_idBooking
inner join Questionaires q on
q.idQuestionaires = b.Questionairs_idQuestionairs
where
b.accepted = 0;
Just a note, you should really change your naming conventions.
For example booking is singular, yet customers is plural in your schema. They should be consistent. Also, something like idBooking and booking_idBooking is not great - maybe use id and booking_id. Anyway just suggestions.
You also might want to invest some time in learning how to write joins. Checkout w3schools for good examples.
SELECT b.*,q.*,c.*
FROM Booking AS b
JOIN Questionaries AS q ON q.idQuestionaries=b.Questionaries_idQuestionaries
JOIN Booking_has_Customers AS bhc ON bhc.Booking_idBooking=b.idBooking
JOIN Customers AS c ON c.idCustomers=bhs.Customers_idCustomers
WHERE b.accepted=0
But come on, change ID names into simple id and you'll have:
SELECT b.*,q.*,c.*
FROM Booking AS b
JOIN Questionaries AS q ON q.id=b.id
JOIN Booking_has_Customers AS bhc ON bhc.id=b.id
JOIN Customers AS c ON c.id=bhs.id
WHERE b.accepted=0
What is the simplest way to make part of a WHERE clause dependent on the result of a JOIN? I realize that is an extremely ambiguous and confusing question, so allow me to simply show you what I am trying to achieve:
SELECT m.member_id, m.first_name, m.last_name
FROM cal_form_answers a
INNER JOIN cal_form_elements e
USING(element_id)
INNER JOIN cal_forms f
USING(form_id)
LEFT JOIN members m
USING(member_id)
WHERE f.org_id = ?
AND m.org_id = ?
AND e.form_id = ?
GROUP BY a.member_id
ORDER BY a.member_id
First, note that the question marks are not invalid syntax—I am using Codeigniter, which uses them as bound parameters.
Line 10 (AND m.org_id = ?) is dependent on whether or not the LEFT JOIN actually finds something. If there is no match in the members table, then Line 10 becomes unnecessary. In fact, it becomes a problem. I would like to limit results by Line 10 unless there is no match in the members table. In that event, I would simply like to ignore that part of the WHERE clause.
I believe this can be achieved using subqueries, though I am admittedly unsure how to work out the syntax. Is there any other way? If yes, how else can this result be achieved? In the event there is no other way, can somebody demonstrate an implementation of a subquery for this situation, and explain how it works?
Thank you!
If a LEFT JOIN does not match a record, then those LEFT JOINed fields are null. Why not just check if that field IS NULL, and when it is not then check it.)
SELECT m.member_id, m.first_name, m.last_name
FROM cal_form_answers a
INNER JOIN cal_form_elements e
USING(element_id)
INNER JOIN cal_forms f
USING(form_id)
LEFT JOIN members m
USING(member_id)
WHERE f.org_id = ?
AND (m.org_id = ? OR m.org_id IS NULL)
AND e.form_id = ?
GROUP BY a.member_id
ORDER BY a.member_id
SELECT a.acikkapali,
b.onay, b.evrakno,
b.tarih,
a.kod,
d.ad,
a.note0,
a.sf_miktar,
a.sf_sf_unit,
a.rtalepedilentestarih,
c.evrakno
FROM stok47T a
LEFT JOIN stok47e b on a.evrakno = b.evrakno
LEFT JOIN stok46t1 c on a.evrakno = c.talepno
LEFT JOIN stok00 d on a.kod = d.kod
WHERE a.tarih = '2013/04/15'
I need to add two my tables into that script with right way that means If I mapped one of them then the normal row count increases this makes me crazy, I have been trying to solve that issue for a couple days but I had been fail many times.
I couldn't find a good mapped fields between stok47t and the others. But there are still a few columns(fields) matches for their types and data.
I need to listen ppl opinions and learns something.
Here is a big part of my query
If you are getting increase in row count then chances are it could be due to using LEFT JOIN. An INNER join might help (see http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html guidance)
SELECT a.acikkapali,
b.onay, b.evrakno,
b.tarih,
a.kod,
d.ad,
a.note0,
a.sf_miktar,
a.sf_sf_unit,
a.rtalepedilentestarih,
c.evrakno
FROM stok47T a
INNER JOIN stok47e b on a.evrakno = b.evrakno
INNER JOIN stok46t1 c on a.evrakno = c.talepno
INNER JOIN stok00 d on a.kod = d.kod
WHERE a.tarih = '2013/04/15'
However without understanding your data structure then there is a chance you might lose the information that you are after.
If you are getting multiple rows, it is probably due to a Cartesian product occurring in the joins. This is unrelated to the type of join (left/right/full/inner). It is based on the relationships between the tables. You have 1-N relationships along different dimensions.
Your conditions are:
FROM stok47T a
LEFT JOIN stok47e b on a.evrakno = b.evrakno
LEFT JOIN stok46t1 c on a.evrakno = c.talepno
LEFT JOIN stok00 d on a.kod = d.kod
I have no idea what these tables and fields mean. But, if you have a case where there is one row per evrakno in table stok47t, and there are two rows in table stok47e and three in table stok46t1, then you will get six rows in the output.
Without more information, it is impossible to tell you the best solution. One method is to summarize the tables. Another is to take the first or last corresponding row, by doing something like:
from stok47T a left join
(select s.*, row_number() over (partition by evrakno order by id desc) as seqnum
from stok47e s
) b
on a.evrakno = b.evrakno and b.seqnum = 1
I am using the following mysql select-query with several joins. I am wondering if this is how a somewhat good select-statement should look like:
SELECT *
FROM table_news AS a
INNER JOIN table_cat AS b ON a.cat_id = b.id
INNER JOIN table_countries AS c ON a.country_id = c.id
INNER JOIN table_addresses AS d ON a.id = d.news_id
WHERE a.deleted = 0
AND a.hidden = 0
AND a.cat_id = ".$search_cat."
AND a.country_id = ".$search_country."
AND a.title LIKE '%".$search_string."%'
OR a.deleted = 0
AND a.hidden = 0
AND a.cat_id = ".$search_cat."
AND a.country_id = ".$search_country."
AND a.subtitle LIKE '%".$search_string."%'"
It seems to be a lot of joins. Even though table b and table c contain only 3 or 4 fields, I wonder if the number of joins would clearly slow down the search on the starting-page?
Would it be better to put the fields from table d (street, city and so on) back into the main-table, as they should be needed most of the time this query is executed?
Thanx in advance,
Jayden
I don't think there is necessarily anything wrong with having three joins. There are a couple of things you can do to make sure the query is optimised.
Firstly, you should never do SELECT * - instead explicitly state what fields you want to return from the database.
Also, I would create indexes on all the fields you have in the where clause, and all of the fields you are joining. This can be a little bit of a trade off - for example if you are doing a lot of write operations then there is a hit because you need to write to the index everytime.
Probably a very basic solution to this, but I can't seem to figure it out. I'm trying to match
multiple ID's in one table towards another.
Structure is something like this,
tt_staff (ID - name)
2 - Lenny
3 - Carl
tt_run (producer1 - producer2)
2 (i.e Lenny)
3 (i.e Carl)
I want to create a view with a single row that shows me the names of both Carl and Lenny, rather then their ID's.
Tried the following,
SELECT e.*, run.*, s.s_name FROM tt_run AS run, tt_events AS e, tt_staff AS s
WHERE e.e_ID = run.e_ID
AND run.e_bandproducer1 = s.s_ID
AND run.e_bandproducer2 = s.s_ID
This obviously doesn't work, since the ID is found on producer1. I've also tried with UNION, but not familiar enough with it (I did manage to get the right result, but in two rows).
As always, thanks for any replies.
It looks like you just need to join tt_staff twice:
SELECT e.*, r.*, s1.s_name, s2.s_name
FROM tt_events e
INNER JOIN tt_run r ON e.e_ID = r.e_ID
INNER JOIN tt_staff s1 ON r.e_bandproducer1 = s1.s_ID
INNER JOIN tt_staff s2 ON r.e_bandproducer2 = s2.s_ID