Need help with this query, cannot get it work right
SELECT a.code, a.description, a.adjustment FROM activity AS a
WHERE a.pid = $customer_id AND a.consult = $consultation_id AND a.code LIKE $code
$code equals can be a something like this "12043 OFFICE CONSULT' (of course different numbers for different descriptions) OR 'DE-POSIT' OR 'UNKNOWN'
the problem is that DBs store this like "12043 OFF" OR UNKNOWN. My query only matches the unknown but not the ones with the number. It can match a number if I do
a.code REGEXP '^[0-9]+' but matches any number not exact match.
If there are multiple lines with different numbers it puts the first match on all the lines
try using LIKE CONCAT('%', $code ,'%')
SELECT a.code, a.description, a.adjustment FROM activity AS a
WHERE a.pid = $customer_id AND a.consult = $consultation_id AND a.code LIKE CONCAT('%', $code ,'%')
Can you try , LIKE '%$code%' it fetches the record it contains the code value.
"SELECT a.code, a.description, a.adjustment FROM activity AS a
WHERE a.pid = $customer_id AND a.consult = $consultation_id AND a.code LIKE '%$code%'";
Ref: http://www.tutorialspoint.com/mysql/mysql-like-clause.htm
Related
I want to match the names columns but some of them contain nicknames for the first names, so I want to do something where I use substring_index to find the first names and whichever table has the shorter first name (e.g. Zach vs Zachary) I match on the length of the shorter first name. After that I match on the last four letters of their last name which I didn't include below. Right now, it's giving me an error near char_length.
SELECT a.name, b.name
FROM a, b
WHERE IF (
a.name < b.name)
THEN
a.name = SUBSTRING(b.name, 1, char_length(a.name))
ELIF (
b.name < a.name)
THEN
b.name = SUBSTRING(a.name, 1, CHAR_LENGTH(b.name))
SELECT CASE WHEN
CHAR_LENGTH(a.name) < CHAR_LENGTH(b.name)
THEN
SUBSTR(b.name, 1, CHAR_LENGTH(a.name))
ELSE
SUBSTR(a.name, 1, CHAR_LENGTH(b.name))
END
FROM a, b
Switch to modern, explicit JOIN syntax.
Use LIKE twice, to find a-names starting with b-names, or b-names starting with a-names.
SELECT a.name, b.name
FROM a
JOIN b
ON a.name like concat(b.name, '%') or b.name like concat(a.name, '%')
I have a big problem understanding why a query works when using LIKE in query and not working when using NOT LIKE.
I will post query below:
select DISTINCT (mails), name
from disposable
JOIN (
SELECT DISTINCT (mail) as mails,
CONCAT(toys.firstname, ' ' , toys.lastname) as name
FROM toys2
join toys ON toys.userid = toys2.id
where ( (toyid = '27' or toyid = '29')
and status != 'Sold'
and toys.regdate >= '2017-01-01'
)
) as tab
WHERE tab.mails LIKE CONCAT('%#', disposable.email)
I think what you want is something more like the following. Note that I simplified the schema a bit so as to do a bit less work for the SQL Fiddle.
SELECT c.email, c.name
FROM customer c LEFT JOIN disposable d
ON SUBSTR(c.email, INSTR(c.email, '#')+1, LENGTH(c.email)-INSTR(c.email, '#')) = d.email
WHERE d.email IS NULL;
Basically, here you're getting the domain of the customer and matching it to the entry in the disposable table. The final WHERE clause uses IS NULL to determine the customer email addresses that are not disposable - use IS NOT NULL to find the ones that are.
Hope this helps.
I need to query two columns "oc_filter_group_description.name" and "oc_filter.name" and get all matching results.
The alias seems to be causing me the problem. I've tried using a sub query but being a complete novice I really don't know what I'm doing.
Here is the code as it stands. The OR clause is what needs adapting
SELECT *,(SELECT name FROM oc_filter_group_description fgd
WHERE f.filter_group_id = fgd.filter_group_id) AS `group`
FROM oc_filter f LEFT JOIN oc_filter_description fd ON (f.filter_id = fd.filter_id)
WHERE fd.name LIKE '% **QUERY GOES HERE** %'
OR 'group' LIKE '% **QUERY GOES HERE** %'
I have spent far too long trying to get this to work I thought I better call in re-enforcements. Thanks in advance
You can't reference column aliases defined in the select in the where clause. This is your query:
SELECT *,
(SELECT name
FROM oc_filter_group_description fgd
WHERE f.filter_group_id = fgd.filter_group_id
) AS `group`
FROM oc_filter f LEFT JOIN
oc_filter_description fd
ON f.filter_id = fd.filter_id
WHERE fd.name LIKE '% **QUERY GOES HERE** %' OR
'group' LIKE '% **QUERY GOES HERE** %';
It has two obvious problems. The first is that 'group' is a string constant not a column reference. Only use single quotes for string literals. It avoids this type of problem. The second is that it refers to the group defined in the select clause.
The best solution is to replace the subquery in the select with another join:
SELECT *, fgd.name as group_name
FROM oc_filter f LEFT JOIN
oc_filter_description fd
ON f.filter_id = fd.filter_id LEFT JOIN
oc_filter_group_description fgd
ON f.filter_group_id = fgd.filter_group_id
WHERE fd.name LIKE '% **QUERY GOES HERE** %' OR
fgd.name LIKE '% **QUERY GOES HERE** %';
Note that I also changed the name group to group_name, so it doesn't conflict with a MySQL reserved words. Voila, no quotes are needed.
This might look a minor thing, but you shouldn't use backticks and whenever possible avoid keywords:
SELECT *, (
SELECT name FROM oc_filter_group_description fgd
WHERE f.filter_group_id = fgd.filter_group_id) AS T_GROUP
FROM oc_filter f LEFT JOIN oc_filter_description fd ON (f.filter_id = fd.filter_id)
WHERE fd.name LIKE '% **QUERY GOES HERE** %'
OR T_GROUP LIKE '% **QUERY GOES HERE** %'
Hope this helped.
You made it much more complicated than necessary. All you need is something like this:
select just the fields you need
from oc_filter f LEFT JOIN oc_filter_description fd ON (f.filter_id = fd.filter_id)
and (fd.name like '%static text goes here, not a query%'
or `group` like '%static text goes here, not a query%')
Notice that the filters are now part of the join as opposed to a where clause. If they were in a where clause, the left join would effectively become an inner join.
I have 3 tables.
First: "atributy"
Second: "atributy_value"
Third: "produkty"
I have this query first:
SELECT a.*, p.ATTRIBUTE_CODE, p.ATTRIBUTE_VALUE, p.KATEGORIA
FROM atributy a JOIN produkty p ON p.ATTRIBUTE_CODE
LIKE CONCAT('%', a.code, '%')
AND
KATEGORIA IN ('$kategoria_sql')
GROUP BY a.value
And my second query is this:
SELECT * FROM atributy_value
INNER JOIN produkty
ON produkty.ATTRIBUTE_VALUE LIKE CONCAT('%', atributy_value.ValueCode, '%')
AND AttributeCode = '$atribut_kod'
AND KATEGORIA IN ('$kategoria_sql')
GROUP BY atributy_value.Value
Help me please make from this 2 query's 1 one better.
Reason: too long loading my web e-shop.
EDIT:
$query = mysql_query("
SELECT a.*, p.ATTRIBUTE_CODE, p.ATTRIBUTE_VALUE, p.KATEGORIA
FROM atributy a JOIN produkty p ON p.ATTRIBUTE_CODE LIKE CONCAT('%', a.code, '%')
AND KATEGORIA IN ('$kategoria_sql')
GROUP BY a.value ");
while($result = mysql_fetch_object($query)){
$atribut_kod = $result->code;
$atribut_value = $result->value;
$nazov_produktu = $result->NAZOV;
$value1 = $result->ATTRIBUTE_VALUE;
$value1 = explode(" ", $value1);
$value1_count = count($value1);
echo "<div class=\"parametre_panel\">
<h3>".$atribut_value."</h3>
";
$url_kody .= "$atribut_kod,";
$hodnoty_qry = mysql_query("
SELECT * FROM atributy_value
INNER JOIN produkty ON produkty.ATTRIBUTE_VALUE LIKE CONCAT('%', atributy_value.ValueCode, '%')
AND AttributeCode = '$atribut_kod'
AND KATEGORIA IN ('$kategoria_sql')
GROUP BY atributy_value.Value ");
while($hodnoty_res = mysql_fetch_object($hodnoty_qry)){
$cislo_hodnoty = $hodnoty_res->ValueCode;
echo "<input type=\"checkbox\" class=\"ZobrazParametrickeVyhladavanie\" name=\"value[]\" id=\"$cislo_hodnoty\" value=\"".$atribut_kod."-".$cislo_hodnoty."\"><label for=\"$cislo_hodnoty\">".$hodnoty_res->Value."</label>
";
$url_hodnoty .= "$cislo_hodnoty,";
} //second query while()
echo "</div>";
} //first query while()
EDIT 2:
My table structure
produkty: http://i.imgur.com/J4Kz2CE.png
atributy_value: http://i.imgur.com/nX1uRph.png
atributy: http://i.imgur.com/mlCa3It.png
Indexes:
atributy: http://i.imgur.com/ppMEEOe.png
atributy_value: http://i.imgur.com/RHAeSiu.png
produkty: http://i.imgur.com/IUrgy9l.png
You can try Below Query:
SELECT a.*,
p.ATTRIBUTE_CODE,
p.ATTRIBUTE_VALUE,
p.KATEGORIA
FROM atributy a
JOIN (SELECT *
FROM atributy_value
INNER JOIN produkty
ON produkty.ATTRIBUTE_VALUE LIKE CONCAT('%', atributy_value.ValueCode, '%')
AND AttributeCode = '$atribut_kod'
AND KATEGORIA IN ('$kategoria_sql')
GROUP BY atributy_value.Value) p
ON p.ATTRIBUTE_CODE LIKE CONCAT('%', a.code, '%')
AND KATEGORIA IN ('$kategoria_sql')
GROUP BY a.value
In Inner Query Select Only Required Columns
Hope this works.
1) Run an EXPLAIN on your queries and add indexes where necessary. IMHO, these queries are not meant to run on production. However, I have made a suggestion below.
2) Let MySQL handle the query execution plan for you. Instead of mentioning a JOIN. Try this
$query = mysql_query("
SELECT a.*, p.ATTRIBUTE_CODE, p.ATTRIBUTE_VALUE, p.KATEGORIA
FROM atributy a,produkty p
WHERE p.ATTRIBUTE_CODE LIKE CONCAT('%', a.code, '%')
AND KATEGORIA IN ('$kategoria_sql')
GROUP BY a.value ");
2) Also change the inner query to the following:
SELECT atributy_value.* FROM atributy_value AS atributy_value, produkty AS produkty
WHERE
produkty.ATTRIBUTE_VALUE LIKE CONCAT('%', atributy_value.ValueCode, '%')
AND atributy_value.AttributeCode = '$atribut_kod'
AND produkty.KATEGORIA IN ('$kategoria_sql')
GROUP BY atributy_value.Value ")
I need more details on the table structures though
3) Run an EXPLAIN on the above query. If you have any missing indexes, please add them
Two things are slow here.
First - unnecessary outer / inner loop. It should be possible to do this in a single SQL query, which will be a huge time saving.
Without seeing the definitions of your tables, this is the best I can suggest :
SELECT a.*, p.*, av.*
FROM produkty p
JOIN atributy a ON p.ATTRIBUTE_CODE LIKE CONCAT('%', a.code, '%')
JOIN atributy_value av ON p.ATTRIBUTE_VALUE LIKE CONCAT('%', av.ValueCode, '%')
WHERE
KATEGORIA IN ('$kategoria_sql')
At least it's a starting point to test from.
Second - Joining between the two tables using LIKE '%value%'. Is there not an integer ID that links these two tables together? Or at least, should the text be an exact match and you can remove the '%'s?
EDIT (after adding table structure) :
You have two joins, the first from a nice indexed integer atributy_value.ValueCode to a non-indexed text field produkty.ATTRIBUTE_CODE, the second from a non indexed text field atributy.code to another non-indexed text field produkty.ATTRIBUTE_CODE.
It's not a good table structure, it would be easier and faster if every table had a unique integer ID to join on. But you might not have time to change this.
Can you just remove the %'s in your original queries and insist on an exact match between the columns?
To keep it simple, you could just replace :
LIKE CONCAT('%', a.code, '%')
replace with
= a.code
and
LIKE CONCAT('%', atributy_value.ValueCode, '%')
replace with
= CONVERT( varchar(500), atributy_value.ValueCode)
This will make it faster. If it's still not enough you could add indexes on Produkty.ATTRIBUTE_CODE and Produkty.ATTRIBUTE_VALUE and atributy.code.
I have produced the following query.
SELECT t.id AS playerid,
dp.first_name,
dp.surname
FROM ".TBL_FOOT_CAREER_TEAMS." t
INNER JOIN ".TBL_FOOT_CAREER_DB_PLAYERS." dp
ON dp.id = t.playerid
WHERE t.careerid = '$career'
AND (dp.first_name LIKE '%{$keyword[$i]}%')
OR (dp.surname LIKE '%{$keyword[$i]}%')
OR (`dp.first_name + dp.surname` LIKE '%{$keyword[$i]}%')
There are two columns in the database. first_name and surname. As you can see, I'm trying to check if the keyword is in either of those columns. I also try and make them into one complete name and check if that's what the search term is aswell.
I'm getting an error so I can assume this isn't the way to do it!!
Can someone help :)
Thanks
SELECT t.id AS playerid,
dp.first_name,
dp.surname
FROM ".TBL_FOOT_CAREER_TEAMS." t
INNER JOIN ".TBL_FOOT_CAREER_DB_PLAYERS." dp
ON dp.id = t.playerid
WHERE (t.careerid = '$career') AND
(
(dp.first_name LIKE concat('%', $keyword[$i], '%')) OR
(dp.surname LIKE concat('%', $keyword[$i], '%')) OR
(CONCAT(dp.first_name, ' ',dp.surname) LIKE concat('%', $keyword[$i], '%'))
)
UPDATE
since you are concactenating the name, you can it like this:
SELECT t.id AS playerid,
dp.first_name,
dp.surname
FROM ".TBL_FOOT_CAREER_TEAMS." t
INNER JOIN ".TBL_FOOT_CAREER_DB_PLAYERS." dp
ON dp.id = t.playerid
WHERE (t.careerid = '$career') AND
(
CONCAT(dp.first_name, ' ',dp.surname) LIKE concat('%', $keyword[$i], '%')
)
Use CONCAT() in your query: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
...
OR CONCAT(dp.first_name, dp.surname) LIKE '%{$keyword[$i]}%'