I have a list of categories that are saved in a string. One is on an article table the other on an ad table. I need the script to return all rows that have a combination of any of these categories between the two tables.
Category string on both tables:
Civic,Community,Sports,Business
And my MySQL script
SELECT `Ad_ID`, `Ad_URL`, `Ad_Image`, `Ad_Type`, `Ad_Cities`, `Ad_Categories`
FROM `Ad_TABLE`
INNER JOIN `Article_TABLE` ON `Article_TABLE`.`Article_Cat` = `Ad_TABLE`.`Ad_Cat`
WHERE Article_TABLE.Article_Cat LIKE '%Civic%'
OR Article_TABLE.Article_Cat LIKE '%Community%'
OR Article_TABLE.Article_Cat LIKE '%Sports%'
OR Article_TABLE.Article_Cat LIKE '%Business%'
AND Ad_TABLE.Ad_Cat LIKE '%Civic%'
OR Ad_TABLE.Ad_Cat LIKE '%Community%'
OR Ad_TABLE.Ad_Cat LIKE '%Sports%'
OR Ad_TABLE.Ad_Cat LIKE '%Business%'
The script only returns records that are only in one of these categories, but there are records that are in multiple categories and I need it to return those as well.
How can I get it to where it finds all matching categories between the two tables?
I suspect you need to add parenthesis to the WHERE clause:
SELECT `Ad_ID`, `Ad_URL`, `Ad_Image`, `Ad_Type`, `Ad_Cities`, `Ad_Categories`
FROM `Ad_DB`
INNER JOIN `Article_DB` ON `Article_DB`.`Article_Cat` = `Ad_DB`.`Ad_Cat`
WHERE (Article_DB.Article_Cat LIKE '%Civic%'
OR Article_DB.Article_Cat LIKE '%Community%'
OR Article_DB.Article_Cat LIKE '%Sports%'
OR Article_DB.Article_Cat LIKE '%Business%')
AND (Ad.DB_Cat LIKE '%Civic%'
OR Ad.DB_Cat LIKE '%Community%'
OR Ad.DB_Cat LIKE '%Sports%'
OR Ad.DB_Cat LIKE '%Business%')
I'm not sure exactly how your tables are structured, but this query will return rows where Article_DB.Article_Cat AND Ad.DB_Cat contain one of your categories. You likely want to rethink the way you store data in these tables so that you're not duplicating data.
Here's a guess. This is just a guess, based on a possible interpretation of the nebulous specification.
To "match" rows that have one or more of these four categories in common
Civic,Community,Sports,Business
We could use a query with join predicates like this:
SELECT ...
FROM `Ad_DB` d
JOIN `Article_DB` r
ON ( FIND_IN_SET('Civic' ,d.ad_categories)
AND FIND_IN_SET('Civic' ,r.article_cat )
)
OR ( FIND_IN_SET('Community' ,d.ad_categories)
AND FIND_IN_SET('Community' ,r.article_cat )
)
OR ( FIND_IN_SET('Sports' ,d.ad_categories)
AND FIND_IN_SET('Sports' ,r.article_cat )
)
OR ( FIND_IN_SET('Business' ,d.ad_categories)
AND FIND_IN_SET('Business' ,r.article_cat )
)
ORDER BY ...
NOTE: I understand that we get what we get, and sometimes we get handed a database that stores comma separated lists.
Storing comma separated lists in a column is a SQL Antipattern. For anyone that has an interest as to why it's an antipattern, I recommend Chapter 2 of Bill Karwin's excellent book
https://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557
Let me start by saying that I am new to mysql and have searched on this site and others for a solution that will work for me. I've gotten fairly close (I think), but can't seem to find a solution.
I'm working on a Joomla site using RSForms software. I need to search a table where one row contains FirstName, another row contains LastName, and another row contains Email. Ultimately, I am looking for SubmissionId that is common to all 3. SubmissionId is a number generated by some form software that groups all of the form submission's elements by this id. I need to search for this id that is common to all 3. Each of the 3 elements may contain duplicates of the search. For instance, searching for the first name "John" will likely produce multiple results. I want to find the SubmissionId that matches "John" and last name "Doe" and email "johndoe#gmail.com" so I can use that to look up other information. It is also possible that there could be multiple matches for "John", "Doe", and johndoe#gmail.com".
I've tried many variations of the following (with/without ANY) and putting the results into an array and counting through each element. I've only had success in acquiring the first instance of "John".
$getresults = $db->setQuery("SELECT SubmissionId FROM my_table_values WHERE FormId = '$formid' AND FieldValue = '$fname' AND SubmissionId = ANY
(SELECT SubmissionId FROM my_table_values WHERE FormId = '$formid' AND FieldValue = '$lname' AND SubmissionId = ANY
(SELECT SubmissionId FROM my_table_values WHERE FormId = '$formid' AND FieldValue = '$email' ))");
---SubmissionValueId---|---FormId---|---SubmissionId---|---FieldName---|---FieldValue---
---------18192---------|-----20-----|-------5462-------|-----Email-----|---johndoe#gmail.com---
---------18193---------|-----20-----|-------5462-------|-----FName-----|---John---
---------18194---------|-----20-----|-------5462-------|-----LName-----|---Doe---
One possibility is to use a query that joins my_table_values with itself, even two times, to create something that can then be checked. For instance ... (fair warning: extemporaneous coding) ...
select A.submission_id from my_table_values A inner join my_table_values B using (SubmissionID) inner join my_table_values C using (SubmissionID) where (A.formID="email" and A.fieldValue= "foo#bar.com") and (B.formID="lastname" and B.fieldValue= "bar") and (C.formID="first name" and C.fieldValue="bletch")
You will need to examine that query to see (a) if the SQL processor accepts it, and (b) what sort of execution-plan it came up with. Preferably, all of the fields should be indexed. You need to see that it plans to use the indexes each time to narrow the list of possibilities, and that it plans to use the most-selective field (e.g. "email") first.
(The using (fieldname) syntax is specific to MySQL. You might need to use on to do the same thing.)
$getresults = $db->setQuery("
SELECT
MAX(IF(FieldValue = '$fname',SubmissionId, NULL) as fnameId,
MAX(IF(FieldValue = '$lname',SubmissionId, NULL) as lnameId,
MAX(IF(FieldValue = '$email',SubmissionId, NULL) as emailId
FROM my_table_values
WHERE FormId = '$formid'
GROUP BY FormId");
Good evening guys,
I'm a newbie to web programming and I need your help to solve a problem inherent to SQL query.
The database engine I'm using is MySQL and I access it via PHP, here I'll explain a simplified version of my database, just to fix ideas.
Let's suppose to work with a database containing three tables: teams, teams_information, attributes. More precisely:
1) teams is a table containing some basic information about italian football teams (soccer, not american football :D), it is formed by three fields: 'id' (int, primary key), 'name' (varchar, team name), nickname (Varchar, team nickname);
2) attributes is a table containing a list of possible information about a football team, such as city (the city where team plays its home match), captain (team captain's fullname), f_number (number of fans) and so on. This table is formed by three fields: id (int, primary key), attribute_name (varchar, an identifier for the attribute), attribute_desc (text, an explanation of the meaning of attribute). Each record of this table represents a single possible attribute of a football team;
3) teams_information is a table where some information, about teams listed in team table, are available. This table contains three fields: id (int, primary key), team_id (int, a foreign key which identifies a team), attribute_id (int, a foreign key which identifies one of the attributes listed in attributes table), attribute_value (varchar, the value of the attribute). Each record represents a single attribute of a single team. In general, different teams will have a different number of information, so for some teams a large number of attributes will be available while for other teams only a small number of attributes will be available.
Note that relation between teams and teams_information is one to many and the same relation exists between attributes and teams_information
Well, given this model my purpose is to realize a grid (maybe with ExtJS 4.1) to show user the list of italian football team, each record of this grid will represent a single football team and will contain all possible attributes: some fields may be empty (because, for considered team, the correspondent attribute is unknown), while the others will contain the values stored in teams_information table (for the considered team).
According to the above grid's field are: id, team_name and a number of fields to represent all the different attributes listed in 'attributes' table.
My question is: can I realize such a grid by using a SINGLE SQL query (maybe a proper SELECT query, to fetch all data I need from database tables) ?
Can anyone suggest me how to write a similar query (if it exists) ?
Thanks in advance for helping me.
Regards.
Enrico.
The short answer to your question is no, there is no simple construct in MySQL to achieve the result set you are looking for.
But it is possible to carefully (painstakingly) craft such a query. Here is an example, I trust you will be able to decipher it. Basically, I'm using correlated subqueries in the select list, for each attribute I want returned.
SELECT t.id
, t.name
, t.nickname
, ( SELECT v1.attribute_value
FROM team_information v1
JOIN attributes a1
ON a1.id = v1.attribute_id AND a1.attribute_name = 'city'
WHERE v1.team_id = t.id ORDER BY 1 LIMIT 1
) AS city
, ( SELECT v2.attribute_value
FROM team_information v2 JOIN attributes a2
ON a2.id = v2.attribute_id AND a2.attribute_name = 'captain'
WHERE v2.team_id = t.id ORDER BY 1 LIMIT 1
) AS captain
, ( SELECT v3.attribute_value
FROM team_information v3 JOIN attributes a3
ON a3.id = v3.attribute_id AND a3.attribute_name = 'f_number'
WHERE v3.team_id = t.id ORDER BY 1 LIMIT 1
) AS f_number
FROM teams t
ORDER BY t.id
For 'multi-valued' attributes, you'd have to pull each instance of the attribute separately. (Use the LIMIT to specify whether you are retrieving the first one, the second one, etc.)
, ( SELECT v4.attribute_value
FROM team_information v4 JOIN attributes a4
ON a4.id = v4.attribute_id AND a4.attribute_name = 'nickname'
WHERE v4.team_id = t.id ORDER BY 1 LIMIT 0,1
) AS nickname_1st
, ( SELECT v5.attribute_value
FROM team_information v5 JOIN attributes a5
ON a5.id = v5.attribute_id AND a5.attribute_name = 'nickname'
WHERE v5.team_id = t.id ORDER BY 1 LIMIT 1,1
) AS nickname_2nd
, ( SELECT v6.attribute_value
FROM team_information v6 JOIN attributes a6
ON a6.id = v6.attribute_id AND a6.attribute_name = 'nickname'
WHERE v6.team_id = t.id ORDER BY 1 LIMIT 2,1
) AS nickname_3rd
I use nickname as an example here, because American soccer clubs frequently have more than one nickname, e.g. Chicago Fire Soccer Club has nicknames: 'The Fire', 'La Máquina Roja', 'Men in Red', 'CF97', et al.)
NOT AN ANSWER TO YOUR QUESTION, BUT ...
Have I mentioned numerous times before, how much I dislike working with EAV database implementations? What should IMO be a very simple query turns into an overly complicated beast of a potentially light dimming query.
Wouldn't it be much simpler to create a table where each "attribute" is a separate column? Then queries to return reasonable result sets would look more reasonable...
SELECT id, name, nickname, city, captain, f_number, ... FROM team
But what really makes me shudder is the prospect that some developer is going to decide that the LDQ should be "hidden" in the database as a view, to enable the "simpler" query.
If you go this route, PLEASE PLEASE PLEASE resist any urge you may have to store this query in the database as a view.
I'm going to take a slightly different route. Spencer's answer is fantastic, and it addresses the issue quite well, but there's still a large underlying problem.
The data that you are trying to display on the site is over-normalized in the database. I won't elaborate, since, again, Spencer's answer highlights the issue pretty well.
Rather, I'd like to recommend a solution that denormalizes the data a bit.
Convert all of your Team data into a single table with many columns. (If there is Player data that isn't covered in the question, that would be a second table, but I'll gloss over that for now.)
Sure, you'll have a whole bunch of columns, and a lot of the columns might be NULL for a lot of the rows. It's not normalized, and it's not pretty, but here's the huge advantage that you gain.
Your query becomes:
SELECT * FROM Teams
That's it. That gets displayed right to the website and you are done. You might have to go out of your way to realize this schema, but it would be totally worth the time investment.
I think what you're saying is that you want the rows in the attributes table to appear as columns in the result recordset. If this is correct, then then in SQL you would use PIVOT.
A quick search on SO seems to indicate that there is no PIVOT equivalent in MySql.
I wrote a simple PHP script to generalize spencer's idea to solve my issue.
Here's the code:
<?php
require_once('includes/db.config.php'); //this file performs connection to mysql
/*
* Following function requires a table name ($table)
* and a number of service fields ($num). Given those parameters
* it returns the number of table fields (excluding service fields).
*/
function get_fields_number($table,$num,$conn)
{
$query = "SELECT * FROM $table";
$result = mysql_query($query,$conn);
return mysql_num_fields($result)-$num; //remember there are $num service fields
}
/*
* Following function requires a table name ($table) and an array
* containing a list of service fields names. Given those parameters,
* it returns the list of field names. That list is contained within an array and
* service fields are excluded.
*/
function get_fields_name($table,$service,$conn)
{
$query = "SELECT * FROM $table";
$result = mysql_query($query,$conn);
$name = array(); //Array to be returned
for ($i=0;$i<mysql_num_fields($result);$i++)
{
if(!in_array(mysql_field_name($result,$i),$service))
{
//currently selected field is not a service field
$name[] = mysql_field_name($result,$i);
}
}
return $name;
}
//Below $conn is db connection created in 'db.config.php'
$query = "SELECT `name` FROM `detail_arg` WHERE visibility = 0";
$res = mysql_query($query,$conn);
if($res===false)
{
$err_msg = mysql_real_escape_string(mysql_error($conn));
echo "{success:false,data:'".$err_msg."'}";
die();
}
$arg = array(); //list of argument names
while($row = mysql_fetch_assoc($res))
{
$arg[] = $row['name'];
}
//Following function writes the select subquery which is
//necessary to build a column containing a single attribute.
function make_subquery($attribute) //$attribute contains attribute name
{
$query = "";
$query.="(SELECT incident_detail.arg_value ";
$query.="FROM incident_detail ";
$query.="INNER JOIN detail_arg ";
$query.="ON incident_detail.arg_id = detail_arg.id AND detail_arg.name='".$attribute."' ";
$query.="WHERE incident.id = incident_detail.incident_id) ";
$query.="AS $attribute";
return $query;
}
/*
echo make_subquery("date"); //debug code
*/
$subquery = array(); //list of subqueries
for($i=0;$i<count($arg);$i++)
{
$subquery[] = make_subquery($arg[$i]);
}
$query = "SELECT "; //final query containing subqueries
$fields = get_fields_name("incident",array("id","visibility"),$conn);
//list of 'incident' table's fields
for($i=0;$i<count($fields);$i++)
{
$query.="incident.".$fields[$i].", ";
}
//insert the subqueries
$sub = implode($subquery,", ");
$query .= $sub;
$query.=" FROM incident ORDER BY incident.id";
echo $query;
?>
I need to store a group of "tags" applied to objects. I was thinking of using something along the lines of :
But in order to implement gmail's "Implicit Social Graph" algorithm (see this question), I need to be able to search for groups of tags that contain one or more specific tags.
So I guess my question is how to get the intersection of two groups of items, in mysql, in the most efficient way ?
find all sets containing one specific tag (given the value):
select tags_sets_id
from tags_has_sets, tags
where value = 'foo'
and tags_id = id;
find all sets containing either of two (or more) specific tags (given the values):
select distinct tags_sets_id
from tags_has_sets, tags
where value in ('foo', 'bar')
and tags_id = id;
find all sets containing both of exactly two specific tags:
select t1.tags_sets_id
from tags_has_sets t1, tags tags1,
tags_has_sets t2, tags tags2
where tags1.value = 'foo'
and tags2.value = 'bar'
and t1.tags_id = tags1.id
and t2.tags_id = tags2.id
and t1.tags_sets_id = t2.tags_sets_id;
note that the last solution doesn't generalize, but you could conceivably build a generalized algorithm to generate an n-joined sql statement on the fly.
here is one last implementation that does generalize, although i don't know its performance characteristics compared to the generated-join way (thanks to #ypercube for an excellent enhancement to my initial suggestion):
select tags_sets_id
from tags_has_sets, tags
where value in ('foo', 'bar', 'baz')
and id = tags_id
group by tags_sets_id
having count(*) = 3;
-- formerly: having group_concat(distinct value order by value)
-- ='bar,baz,foo';
I've got 3 dataset objects that are nested with each other using entity set objects. I am selecting the data like this
var newList = from s in MainTable
from a in s.SubTable1 where a.ColumnX = "value"
from b in a.Detail where b.Name = "searchValue"
select new {
ID = s.ID,
Company = a.CompanyName,
Name = b.Name,
Date = s.DueDate
Colour = b.Colour,
Town = a.Town
};
and this works fine, but the trouble is there are many records in the Detail object-list/table for each Name value so I get a load of duplicate rows and thus I only want to display one record per b.Name. I have tried putting
group s by b.Name into g
before the select, but then this seems to stop the select enabling me to select the columns I want (there are more, in practice). How do I use the group command in this circumstance while still keeping the output rows in a "flat" format?
Appending comment as answer to close question:-
Of course that if you group your results, you cant get select a column of a child, thats because there may be more than one childs and you have to specify an aggregate column for example the sum,max etx –