MySQL - alternative for like - regexp? - mysql

i have got over one milion record in my database table.
When I use like is very slowly, when i use match against they lost some records.
I create help table:
tag_list
tag_id
tag_name
tag_rel_message
tag_id
messag_id
messages
message_id
message_text
in tag_list i add all words of $message_text - explode(" ", $message_text);
My new query is:
SELECT m.*
FROM tag_rel_messages trm
INNER JOIN messages m ON (trm.message_id = m.message_id)
INNER JOIN tag_list tl ON (trm.tag_id=tl.tag_id
WHERE tl.tag_name REGEXP 'pionas' AND tl.tag_name REGEXP 'website'
GROUP By trm.message_id
But not display any records.
What's wrong with this query?
Maybe i should use something other than REGEXP?
Thanks for help

I'm not sure how one column could match two things at the same time! You can use OR in place of AND, or you can use a single call to REGEXP.
SELECT m.*
FROM tag_rel_messages trm
INNER JOIN messages m ON (trm.message_id = m.message_id)
INNER JOIN tag_list tl ON (trm.tag_id=tl.tag_id
WHERE tl.tag_name REGEXP 'pionas|website'
GROUP By trm.message_id

You need to join with the tag_list and tag_rel_message tables twice to match two different tags.
SELECT m.*
FROM messages AS m
JOIN tag_rel_messages AS trm1 ON m.message_id = trm1.message_id
JOIN tag_list AS t1 ON t1.tag_id = trm1.tag_id
JOIN tag_rel_messages AS trm2 ON m.message_id = trm2.message_id
JOIN tag_list AS t2 on t2.tag_id = trm2.tag_id
WHERE t1.tag_name = 'pionas' AND t2.tag_name = 'website'
Another way to do it is to join with either tag, and count the number of results per message
SELECT m.*
FROM messages AS m
JOIN tag_rel_messages AS trm ON m.message_id = trm.message_id
JOIN tag_list AS t ON t1.tag_id = trm.tag_id
WHERE t.tag_name IN ('pionas', 'website')
GROUP BY m.message_id
HAVING COUNT(*) = 2
This second form is a little easier to generalize to any number of tags. Put all the tags in the IN clause, and change the HAVING clause to match the number of tags.

$words = array('pionas', 'website');
$tags_id = array();
foreach ($words as $key => $val) {
$sql = "SELECT tag_id FROM tag_list WHERE tag_name LIKE '%".$val."%'";
$records = $db->query($sql);
$tags_id[$key] = array_column($records, "tag_id");
}
$inner_array = array();
foreach ($tags_id as $k => $v) {
$short_name = "trm_".$k;
$inner_array[] = " INNER JOIN tag_rel_message as ".$short_name." ON ( ".$short_name.".message_id = m.message_id AND ".$short_name.".tag_id IN (".implode(", ", $v).")) ";
}
$sql = "SELECT DISTINCT m.* FROM messages m".implode(" ", $inner_array);
I think this is the same:
SELECT * FROM messages WHERE message_text like '%pionas%' AND message_text like '%website%'

Related

How to view data based on user in codeigniter

I want to view data from mysql in codeigniter based on user who is logged in
but I got an error "A Database Error Occurred". I think I wrote the wrong code
t.USER = $this->session->userdata('user_id');
Here's the code:
function get_rekomen()
{
$query = $this->db->query("SELECT
c.*,
t.produk_id,
t.id_transdet,
t.kategori_id,
t.total_qty,
t.USER
FROM
transaksi_detail AS t
LEFT JOIN
(
SELECT
g.id_produk,
p.slug_produk,
p.foto,
p.foto_type,
p.harga_diskon,
p.diskon,
p.harga_normal,
p.judul_produk,
g.kat_id,
k.judul_kategori
FROM
(
SELECT
MAX(m.id_produk) AS id_produk,
m.kat_id
FROM
produk AS m
GROUP BY
m.kat_id
)
AS g
INNER JOIN
produk AS p
ON p.id_produk = g.id_produk
LEFT JOIN
kategori AS k
ON k.id_kategori = g.kat_id
)
AS c
ON c.kat_id = t.kategori_id
WHERE
t.USER = $ this -> session -> userdata('user_id');
<<// i think here's the problem
ORDER BY
total_qty DESC limit 1")->result();
return $query;
}
and the question is what is the correct code?
Here's one solution, but first make sure on your controller the $this->load->library('session'); is loaded or you can just add in on autoload.php.
Also you must add parameter to your model that will handle the user_id in session, so for example here's your model looks like:
function get_rekomen($user_id) { ...your query here... }
Then on condition using the user_id will be
t.USER = $user_id;
So on your controller, you can call your model like:
$user_id = $this->session->userdata('user_id');
$this->ModelClass->get_rekomen($user_id);
Additional: On your login phase, if the user success from logging in, the you must set the user data on session such as this $this->session->set_userdata('user_data', $session_data);
Hope this helps!
Maybe you can use this. You need to define $user_id first before adding them to WHERE statement...
$user_id = $this->session->userdata('user_id');
$sql = "SELECT c.*, t.produk_id, t.id_transdet, t.kategori_id, t.total_qty, t.USER
FROM transaksi_detail AS t
LEFT JOIN(SELECT g.id_produk, p.slug_produk, p.foto, p.foto_type, p.harga_diskon, p.diskon, p.harga_normal, p.judul_produk, g.kat_id, k.judul_kategori
FROM(SELECT MAX(m.id_produk) AS id_produk, m.kat_id
FROM produk AS m
GROUP BY m.kat_id)
AS g
INNER JOIN produk AS p ON p.id_produk = g.id_produk
LEFT JOIN kategori AS k ON k.id_kategori = g.kat_id)
AS c ON c.kat_id = t.kategori_id
WHERE t.USER = '$user_id'
ORDER BY total_qty DESC limit 1";
$query = $this->db->query($sql);
return $query->result();
Hope this can help you...

Mysql how can i sql two tables and count groupid from the second

I'm trying to make an Sql , that get all data from first table "posts_main" and then get the count of comments from second table "posts_comments"
I tried:
$sql = "SELECT * FROM posts_main, count(posts_comments.groupid)
INNER JOIN posts_comments ON posts_comments.groupid = posts_main.id
WHERE posts_main.user_id = '$user_id'
GROUP BY posts_main.id";
Please, how can i do that ?
thanks....
Try with below query.
$sql = "select posts_main.*,
(select groupid from posts_comments where groupid = posts_main.id group by groupid ) as count_group
from posts_main
WHERE posts_main.user_id = '$user_id' ";
Try this with subquery
SELECT posts.*,cnt FROM posts_main
INNER JOIN (select posts_comments.groupid,count(posts_comments.groupid) as cnt
group by posts_comments.groupid)a
ON a.groupid = posts_main.id
WHERE posts_main.user_id = '$user_id'

Query where column value equals count MySQL

I have 3 tables, "negocio" , "paquete" , "posts".
"negocio" has one "paquete", and "negocio" may have one or more "posts".
I want to bring all negocios that have the same amount of "posts" (that have as value on it's column "posts.tipo_post" the value "Post") records as the value on it's respective "paquete.no_posts"
I was doing something like this, but it returns me an empty set.
SELECT DISTINCT negocio.id, negocio.nombre FROM negocio
INNER JOIN posts ON negocio.id = posts.id_negocio
INNER JOIN paquete ON paquete.id_negocio = negocio.id
WHERE paquete.no_posts = (SELECT COUNT(*) FROM negocio INNER JOIN posts
ON posts.id_negocio = negocio.id WHERE posts.tipo_post = 'Post'
AND posts.estado_post = 'Disenador')
Try giving your count(*) table an alias
SELECT COUNT(*) FROM negocio N INNER JOIN posts P
ON P.id_negocio = N.id WHERE P.tipo_post = 'Post'
AND P.estado_post = 'Disenador'

Joining Tables in MySQL Query instead of multiple queries

I have this query
$query = "SELECT * FROM items WHERE itemStatus = '1' AND itemAdded > '$timestamp'";
Once this query has returned results I loop through the results
the results array is itemID, itemLocationID, categoryParentID, categoryID, itemName, itemDetails
During the loop I then run three other queries by calling functions within the same class
$locationName = $this->getLocationByName($locationID);
$categoryParentName = $this->getCategoryByName($categoryParentID);
$categoryName = $this->getCategoryByName($categoryID);
the function getLocationByName performs this query;
$q = mysql_query("SELECT * FROM locations WHERE locationID = '$locationID'");
this returns an array of locationID, locationName, locationLink
the function getCategoryByName performs this query;
$q = mysql_query("SELECT * FROM categories WHERE categoryID = '$categoryID'");
this returns an array of categoryID, categoryName, categoryLink
Could someone please help me optimize this query and maybe join them to save doing so many queries.
thanks in advance.
Im now using this query
$q = mysql_query("SELECT
i.itemID,
i.locationID,
i.categoryParentID,
i.categoryID,
i.itemName,
i.itemDetails,
l.*,
c.*
FROM
items i
inner join locations l on i.locationID = l.locationID
inner join categories c on i.categoryID = c.categoryID
WHERE
itemStatus = '1'
AND itemAdded > '$timestamp'")or die(mysql_error());
and the result is
Array
(
[itemID] => 81300
[locationID] => 17
[categoryParentID] => 21
[categoryID] => 183
[itemName] => blah
[itemDetails] => blah
[locationName] => brilliant it pulls in the location as expected.
[locationLink] => blah
[categoryName] => brilliant it pulls in the category as expected.
[categoryLink] => blah
)
[categoryName] => //these are missing for categoryParentID
[categoryLink] => //these are missing for categoryParentID
I would not use * in the select statement
The Query with the joins could be
SELECT
i.itemID,
i.itemLocationID,
i.categoryParentID,
i.categoryID,
i.itemName,
i.itemDetails,
l.*,
c.*
FROM
items i
inner join locations l on i.itemLocationID = l.locationID
inner join categories c on i.categoryID = c.categoryID
WHERE
itemStatus = '1'
AND itemAdded > '$timestamp'
I hope it be useful for you.
Cheers!
I think should be something similar to below query. I do not see where are you using $categoryParentName .
Using your queries and data:
SELECT * FROM items WHERE itemStatus = '1' AND itemAdded > '$timestamp'
SELECT * FROM locations WHERE locationID = '$locationID'
SELECT * FROM categories WHERE categoryID = '$categoryID'
$locationName = $this->getLocationByName($locationID);
$categoryParentName = $this->getCategoryByName($categoryParentID);
$categoryName = $this->getCategoryByName($categoryID);
Please let me know if this returns expected result set. Hope this helps
SELECT
it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
l.locationID, l.locationName, l.locationLink,
c.categoryID, c.categoryName, c.categoryLink
FROM items it
LEFT JOIN locations l ON l.locationID = it.itemLocationID
LEFT JOIN categories c ON c.categoryID = it.categoryID
WHERE
it.itemStatus = '1'
AND it.itemAdded > '$timestamp'
Update query using categoryParentID - i am not saying is efficient but you can test and optimize as needed.
One option is to update above query - not sure that will work - and for large result sets using OR is not efficient:
LEFT JOIN categories c ON (c.categoryID = it.categoryID OR c.categoryID = it.categoryParentID)
The other option that i see is to get 2 result sets (see below) - one for categId = categId and second for categId = categParentId and combine the result set in one big result set.
SELECT
t.itemID, t.itemLocationID, t.categoryParentID, t.categoryID, t.itemName, t.itemDetails,
l.locationID, l.locationName, l.locationLink,
t.categoryID, t.categoryName, t.categoryLink
FROM
(
SELECT
it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
c.categoryID, c.categoryName, c.categoryLink
FROM items it
INNER JOIN categories c ON c.categoryID = it.categoryID
WHERE
it.itemStatus = '1'
AND it.itemAdded > '$timestamp'
UNION -- [ALL]
SELECT
it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
c.categoryID, c.categoryName, c.categoryLink
FROM items it
INNER JOIN categories c ON c.categoryID = it.categoryParentID
WHERE
it.itemStatus = '1' AND
it.itemAdded > '$timestamp'
) AS t
LEFT JOIN locations l ON l.locationID = t.itemLocationID
Other idea - not tested and assuming that id are int - will have to cast as string / char. There are a few options how you can write this query - if you post a structure table and some dummy data i am sure that someone will create a demo / sqlfiddle.
SELECT
it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
l.locationID, l.locationName, l.locationLink,
c.categoryID, c.categoryName, c.categoryLink
FROM items it
LEFT JOIN locations l ON l.locationID = it.itemLocationID
WHERE
it.itemStatus = '1'
AND it.itemAdded > '$timestamp'
AND c.category ID IN ( SELECT GROUP_CONCAT(categs) FROM (SELECT CONCAT(categoryID, ",", categoryParentID) AS categs FROM items ))
Unless I'm missing something obvious, I'd probably suggest something like this as a first start:
select *
from items i
join locations l
on i.location_id=l.location_id
join categories c
on i.category_id=c.category_id
where item_status='1'
and itemAdded > '$timestamp'

Duplicate column name 'user_id' problem

I'm having a problem trying to count the number of user records according to the user's id, however I'm using a subquery to join 2 tables that one has a count parameter but I get an error saying duplicate column name 'user_id.
The query:
$sql = "SELECT loc.location_id,
COUNT(loc.location_id) AS total_records
FROM locations loc
LEFT JOIN
(
SELECT usr.*,
loc.*
FROM
(
members usr
INNER JOIN locations loc
)
WHERE usr.user_id = " . $user_id . "
AND usr.account_disabled = 0
ORDER BY loc.submit_date DESC
) usr ON (loc.user_id = usr.user_id)";
All I need it is to return the user's info and the total_records count done by the COUNT function.
Cheers.
EDIT:
This is what I get returned for this SQL:
SELECT loc.location_id,
loc.street_name,
loc.city,
loc.state,
loc.county,
loc.country,
usr.user_id,
usr.username,
COUNT(loc.location_id) AS total_records
FROM locations loc
INNER JOIN members usr ON (loc.user_id = usr.user_id)
WHERE loc.user_id = $user_id
AND usr.account_disabled = 0
GROUP BY loc.location_id
It's not exactly clear why you've got the derived resultset or the LEFT JOIN. Try this:
SELECT loc.location_id,
COUNT(loc.location_id) AS total_records
FROM LOCATIONS_TABLE loc
INNER JOIN MEMBERS_TABLE usr
ON (loc.user_id = usr.user_id)
WHERE loc.user_id = $user_id
AND usr.account_disabled = 0
GROUP BY loc.location_id
I think the problem is this part:
SELECT usr.*,
loc.*
Both tables have a user_id