I currently have the following EAV table:
id
field_name
field_value
And a standard table:
id
first_name
last_name
I am joining the standard table onto the EAV table for each value that matches the ID, so my query looks something like this:
SELECT id, first_name, last_name, fieldname1, fieldname2
FROM standard_table
LEFT JOIN EAV AS fieldname1 ON
(fieldname1.id = standard_table.id AND fieldname1.field_name = 'fieldname1')
LEFT JOIN EAV AS fieldname2 ON
(fieldname2.id = standard_table.id AND fieldname2.field_name = 'fieldname2');
This has been working fine, up until today where I now have 62 custom fields in my EAV table, this means that my query is joining onto 62 tables and so hitting the MySQL table join limit and failing.
The whole query seems like a bad way of doing it, how can I rewrite this so it is quicker and doesn't require 62 table joins.
You can also use aggregation for EAV. The query looks like:
SELECT st.id, st.first_name, st.last_name,
MAX(CASE WHEN EAV.field_name = 'fieldname1' THEN fieldname1 END),
MAX(CASE WHEN EAV.field_name = 'fieldname2' THEN fieldname2 END)
FROM standard_table st JOIN
EAV
ON EAV.id = st.id
GROUP BY st.id, st.first_name, st.last_name;
As you get more and more columns, this can perform better than dozens of joins.
The other provided answer was the inspiration for this however I below is the query I actually used:
SELECT st.id, st.first_name, st.last_name,
(CASE WHEN `EAV`.`field_name` = 'fieldname1' THEN `EAV`.`field_value` END) AS 'fieldname1',
(CASE WHEN `EAV`.`field_name` = 'fieldname2' THEN `EAV`.`field_value` END) AS 'fieldname2',
FROM standard_table st JOIN
EAV
ON EAV.id = st.id
GROUP BY st.id;
Related
I have 2 tables, student and grades
student table contains id, name and date_of_birth
grades table contains id, student_id, grade and course
Actual table contain more data.
I have a query like
SELECT s.*, AVG(g.grade) as average_grade
FROM student s LEFT JOIN grade g ON s,id = g.student_id
WHERE g.course = 'mathematics' and s.id = 1
With this I could get the data i needed which are student details and the average grade, then come the problem where when the course = "mathematics" is not found in the grades table, the query will return NULL. My question is, is there a way for me to get the s.id = 1 details together with NULL average instead of all NULL value?
I would prefer if it is able to do it with 1 query, as because in my current I am using subquery and it takes very long to get the data. My main objective is to get more faster speed if you have better way instead of using 1 query feel free to comment your idea. In addition I have tried multiple query and sub query to get all the data but it all take too long.
Move your filter criteria for g.course = 'mathematics' in joining part
SELECT s.*, AVG(g.grade) as average_grade
FROM student s
LEFT JOIN grade g ON s.id = g.student_id AND g.course = 'mathematics'
WHERE s.id = 1
Your query produces result as inner join not left because putting g.course = 'mathematics in where clause turns your left join to inner join, Moving this part in on clause will still return data from student table if there were no rows found from grade table with course = 'mathematics'
If the course is not 'mathematics' you would still get the student data if you put it like this.
SELECT s.*, AVG(g.grade) as average_grade
FROM student s LEFT JOIN grade g ON s,id = g.student_id
WHERE (g.course = 'mathematics' AND s.id = 1) OR s.id = 1
In my query I am listing all of the theater ticket sales and movie ticket sales of different customers. The issue I'm running into is that all of the '0' ticket sales, so those users who haven't boughten a theater ticket or movie ticket is not showing up.
Here's a picture for a visual aspect: table
I believe I need to be doing a union to return the users who haven't boughten any tickets. I just can't seem to figure this out.
Thanks in advance.
Here's my code so far:
select customer.hippcode, customer.LastName, customer.Firstname, customer.Email,
count(ticketdetails.eventtype) as 'Theater Tickets',
0 as 'Movie Tickets'
from customer
inner join ticketdetails on ticketdetails.hippcode = customer.hippcode
where ticketdetails.hippcode is not null
and ticketdetails.eventType ='T'
Group by Customer.hippcode
union
select customer.hippcode, customer.LastName, customer.Firstname, customer.Email,
0 as 'Theater Tickets', count(ticketdetails.eventtype) as 'Movie Tickets'
from customer
inner join ticketdetails on ticketdetails.hippcode = customer.hippcode
where ticketdetails.hippcode is not null
and ticketdetails.eventType ='M'
Group by Customer.hippcode
order by `theater tickets` + `movie tickets` desc;
select
customer.hippcode, customer.LastName, customer.Firstname, customer.Email,
sum(case when ticketdetails.eventtype = 'T' then 1 else 0 end) as TheaterTickets,
sum(case when ticketdetails.eventtype = 'M' then 1 else 0 end) as MovieTickets
from customer
inner join ticketdetails on ticketdetails.hippcode = customer.hippcode
where ticketdetails.hippcode is not null
and ticketdetails.eventType in ('T', 'M')
Group by customer.hippcode, customer.LastName, customer.Firstname, customer.Email
Order by 'TheaterTickets' + 'MovieTickets' desc
inner join => bring the line only if you got a record on both table.
I think you should use a LEFT JOIN somewhere to chose the master table
http://dev.mysql.com/doc/refman/5.7/en/join.html and
http://dev.mysql.com/doc/refman/5.7/en/left-join-optimization.html
I think the last query is the only one you want. A left join is appropriate, but you need to be careful about the where clause:
select c.hippcode, c.LastName, c.Firstname, c.Email,
sum(td.eventtype) as TheaterTickets,
sum(td.eventtype) as MovieTickets
from customer c left join
ticketdetails td
on td.hippcode = c.hippcode and
td.eventType in ('T', 'M')
Group by c.hippcode, c.LastName, c.Firstname, c.Email
Order by count(t.hippcode) desc;
Notes:
Table aliases make the query easier to read and write.
Conditions on ticketdetails go in the on clause, not the where clause.
The condition on td.hippcode is not null is unnecessary, because NULL will not match in the join (note: you might want to check for the customer column).
case is the standard way to do the conditional sum (and hence correct). However, MySQL offers a much simpler and intuitive syntax.
Your order by was doing nothing, because it was adding two strings (hence equivalent to order by 0. Don't use single quotes ever for column names, and you won't have a problem like that.
Here is my query which is taking 17.9397 sec time to get response:
SELECT allbar.iBarID AS iBarID,
allbar.vName AS vName,
allbar.tAddress AS tAddress,
allbar.tDescription AS tDescription,
(SELECT COUNT(*)
FROM tbl_post p
WHERE p.vBarIDs = allbar.iBarID) AS `total_post`,
allbar.bar_usbg AS bar_usbg,
allbar.bar_enhance AS bar_enhance,
(SELECT count(*)
FROM tbl_user
WHERE FIND_IN_SET(allbar.iBarID,vBarIDs)
AND (eType = 'Bartender'
OR eType = 'Bar Manager'
OR eType = 'Bar Owner')) AS countAss,
allbar.eStatus AS eStatus
FROM
(SELECT DISTINCT b.iBarID AS iBarID,
b.vName AS vName,
b.tAddress AS tAddress,
(CASE LENGTH(b.tDescription) WHEN 0 THEN '' WHEN LENGTH(b.tDescription) > 0
AND LENGTH(b.tDescription) < 50 THEN CONCAT(LEFT(b.tDescription, 50),'...') ELSE b.tDescription END) AS tDescription,
b.usbg AS bar_usbg,
b.enhance AS bar_enhance,
b.eStatus AS eStatus
FROM tbl_bar b,
tbl_user u
WHERE b.iBarID <> '-10') AS allbar
I have tried EXPLAIN, here is the result of that:
Can anyone explain me this EXPLAIN result?
You should totaly rewrite that query, it's complete nonsense.
In this part
(SELECT DISTINCT b.<whatever>
FROM tbl_bar b,
tbl_user u
WHERE b.iBarID <> '-10') AS allbar
what you're basically doing is connecting every row from table tbl_bar with every row from tbl_user. Then filter tbl_bar, and when everything is selected (maybe MySQL has to write everything in a temporary table before doing this) return the result set without duplicates. You don't ever want to do that. Especially when you don't even select anything from tbl_user. When there's a connection, specify it. If there's none, don't join those tables or create a connection. I don't know if or how your tables are connected, but it should look something like this:
(SELECT DISTINCT b.<whatever>
FROM tbl_bar b
JOIN tbl_user u ON b.user_id = u.id /*or whatever the connection is*/
WHERE b.iBarID <> '-10') AS allbar
Then you have this ugly subquery.
(SELECT COUNT(*)
FROM tbl_post p
WHERE p.vBarIDs = allbar.iBarID) AS `total_post`,
allbar.bar_usbg AS bar_usbg,
allbar.bar_enhance AS bar_enhance,
which is by the way dependent (see your explain output). Which means, that this subquery is executed for every row of your outer query (yes, the one with the cross join as discussed above). Instead of this subquery, join the table in the outer query and work with GROUP BY.
So far the query should look something like this:
SELECT
b.iBarID AS iBarID,
b.vName AS vName,
b.tAddress AS tAddress,
b.tDescription AS tDescription,
COUNT(*) AS `total_post`,
allbar.bar_usbg AS bar_usbg,
allbar.bar_enhance AS bar_enhance
FROM
tbl_bar b
JOIN tbl_user u ON b.user_id = u.id
JOIN tbl_post p ON p.vBarIDs = b.iBarID
WHERE b.iBarID <> '-10'
GROUP BY b.iBarID
(In fact, this is not really right. Rule is, every column in the SELECT clause should either be in the GROUP BY clause as well or have an aggregate function (like count() or max() applied to it. Otherwise a random row of each group is displayed. But this is just an example. You will have to work out the details.)
Now comes the worst part.
(SELECT count(*)
FROM tbl_user
WHERE FIND_IN_SET(allbar.iBarID,vBarIDs)
AND (eType = 'Bartender'
OR eType = 'Bar Manager'
OR eType = 'Bar Owner')) AS countAss,
allbar.eStatus AS eStatus
The use of FIND_IN_SET() suggests, that you're storing multiple values in one column. Again, you never ever want to do that. Please read this answer to Is storing a delimited list in a database column really that bad? and then redesign your database. I won't help you with this one, as this clearly is stuff for a separate question.
All this didn't really explain the EXPLAIN result. For this question, I would have to write a whole tutorial, which I won't do, since everything is in the manual, as always.
I am currently working on a site utilizing the WP-Property plugin for Wordpress.
Basically I am attempting to display a counter next to a text search link for a particular price range.
Below is the query I am running which does the price range count perfectly fine.
$apartprice1 = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->postmeta WHERE meta_value BETWEEN 50000 AND 74999");
if (0 < $apartprice1) $apartprice1 = number_format($apartprice1);
I would however like to extend on this to display only "Apartments" which are within this particular price range.
Now bear in mind that within the meta_value column the property type values (apartments, houses, freeholds) are also stored.
Any boffins about whom can demystify this for me please?
edit: Some further elaboration below:
Table name is postmeta
meta_id post_id meta_key meta_value
348 41 property_type apartment
358 41 price 698000
So I would like to be able to do a count of a particular price range which is also based on a specific property type
This table structure makes it somewhat difficult/inefficient to do the type of multifacet searching you're looking for. To ensure that all WHERE clauses are met, you'll need to INNER JOIN the meta table to itself. This gives you a temporary data format that's bit more suitable for your query.
SELECT m1.post_id
FROM meta m1
INNER JOIN meta m2 ON m1.post_id = m2.post_id
WHERE m1.meta_key = 'price'
AND m1.meta_value
BETWEEN 50000 AND 7000000 AND m2.meta_key = 'property_type'
AND m2.meta_value = 'apartment'
Keep in mind, for every additional facet you add, you'll need to do an additional INNER JOIN. Also, if you don't have one already, I'd recommend an index on the post_id column.
To get the count, just change what's in the SELECT clause.
SELECT COUNT(*)
FROM meta m1
INNER JOIN meta m2 ON m1.post_id = m2.post_id
WHERE m1.meta_key = 'price'
AND m1.meta_value
BETWEEN 50000 AND 7000000 AND m2.meta_key = 'property_type'
AND m2.meta_value = 'apartment'
If you are looking for displaying the count for each of the property values the partments, houses, freeholds, you should add a GROUP BY property_type
SELECT property_type, COUNT(*)
FROM $wpdb->postmeta
WHERE meta_value BETWEEN 50000 AND 74999
GROUP BY property_type
Update: You have to pivot your rows into columns in order to get the data the way you described like so:
SELECT t.property_type, COUNT(t.post_id) AS 'type count'
FROM
(
SELECT t.post_id,
MAX(CASE WHEN t.meta_key = 'property_type' THEN t.meta_value END) AS property_type,
MAX(CASE WHEN t.meta_key = 'price' THEN CAST(t.meta_value AS DECIMAL) END) AS Price
FROM postmeta t
GROUP BY t.post_id
) t
WHERE t.property_type = 'apartment'
AND t.Price BETWEEN 600000 AND 700000
GROUP BY t.property_type
Note that the meta_value field is supposed to store both a string values for property types as well as numeric values for prices so I CAST it to DECIMAL in order to select it as numeric data type so that you can perform calculation on it later on. I also used MAX as an aggregate function as a work around to pivot the rows.
Here is a demo in SQL fiddle.
This what you usually encounter in such a design model which is what they called Entity–attribute–value model. You can find a lot of useful thread about this data model under the tag eav. You can also find more useful pages about this design model in the following answer
Help me out with this query:
I have 3 tables with this structure.
items_to_groups (item_id | group_id)
item_to_regions (item_id | region_id)
items [a bunch of columns]
I need to select every row on the item table that has an item_id match on item_to_groups table WHERE group = x AND has an item_id match on item_to_regions table WHERE region = y
Currently the code I have is a horrible subquery with loops and all.
What would be a better way of doing this?
I've thought about JOIN and such, but can't really get my head around on how to do it.
SELECT bunch_of_columns
FROM items i
INNER JOIN items_to_groups ig ON i.id=ig.item_id
INNER JOIN items_to_regions ir on i.id=ir.item_d
WHERE ir.region_id=y
AND ig.group_id=x
Have a look at the JOIN documentation on MySQL. Joins are important for relational databases.
As you said you have a hard time grasping joins, have a look at A Visual Explanation of SQL Joins by Jeff Atwood. Maybe it helps.
SELECT colums
FROM items
INNER JOIN items_to_groups ON items.item_id = items_to_groups.item_id AND group_id = x
INNER JOIN items_to_regions ON items.item_id = items_to_regions.item_id AND region_id = y
SELECT * FROM items
JOIN items_to_groups ON (items.item_id = items_to_groups.item_id AND group_id = ?)
JOIN items_to_regions ON (items.item_id = items_to_regions.item_id AND region_id = ?)
GROUP BY items.item_id