Inner join with like clause - mysql

I am using inner join with the like clause ..
My tried sql is
SELECT tbl_songs.id AS sid,
tbl_songs.name AS sname,
tbl_albums.id AS aid,
tbl_albums.name AS aname
FROM tbl_songs
INNER JOIN tbl_albums
ON tbl_songs.albums LIKE '%' + tbl_albums.name + '%';
Its showing me syntax error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '+ tbl_albums.name + '%'' at line 2
Please elaborate reason of syntax error.

you have to form the clause using concat ...
...LIKE CONCAT('%',tbl_albums.name, '%');
there is no + operator like this in mysql

You can use below format in oracle sql:
SELECT tbl_songs.id AS sid,
tbl_songs.name AS sname,
tbl_albums.id AS aid,
tbl_albums.name AS aname
FROM tbl_songs
INNER JOIN tbl_albums
ON tbl_songs.albums LIKE ('%'||tbl_albums.name||'%');

An example of MySQL:
SELECT tbl_songs.bus_name FROM tbl_songs , tbl_albums
WHERE tbl_songs.albums LIKE CONCAT('%',tbl_albums.name, '%');

Related

table1 join table2 on table1.varchar is equal to prefix+table2.numeric_id+suffix

I am writing the following query:
SELECT * FROM notes inner join item_source
on notes.item_ids=CONCAT(CONCAT(',', cast(item_source.id as varchar(10))), ',')
item_source.id is the primary key (numeric, auto_increment) of table item_source.
notes.item_ids is a varchar field of table notes.
I want item_source.id=18 to match notes.item_ids=',18,'.
I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(10))), ',')
What am I doing wrong?
You need to covert it to char data type then u can get result
SELECT * FROM notes inner join item_source
on notes.item_ids=CONCAT(CONCAT(',', cast(item_source.id as char(10))), ',')

MySQL join error

The following query gives me an error in phpmyadmin. It looks syntactically correct to me, and the table/column names match up accordingly. I have tried a number of variations (quoting table names, using as, etc) with no luck.
SELECT *
FROM GROUP
INNER JOIN GROUP_MEMBER ON GROUP.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';
Error I'm getting:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP INNER JOIN GROUP_MEMBER ON GROUP.group_id = GROUP_MEMBER.group_id WHERE ' at line 2
"group" is a sql-keyword, you need to surround it with backticks if you want to use it as tablename.
GROUP is a reserved word in SQL so it's a bad choice for a table name. If you surround it with backticks it might work but I'd really recommend changing that table name.
SELECT *
FROM `GROUP`
INNER JOIN GROUP_MEMBER ON `GROUP`.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';
This is not PHPMyAdmin-specfiic error. The problem you have is using a table name GROUP that matches a MySQL reserved word. If you insist on using such a problematic table name, you need to enclose it with backticks anywhere you might use it.
SELECT *
FROM `GROUP`
INNER JOIN GROUP_MEMBER ON `GROUP`.group_id = GROUP_MEMBER.group_id
WHERE group_owner='test';

MySQL syntax error (in SELECT query)

I have two MySQL tables:
Group(gr_id, gr_name, gr_description, parent_id)
Group_has_User(User_id, Group_id)
I'm trying to execute the query:
SELECT group.gr_id, group.gr_name, group.gr_description, group.parent_id
FROM group, Group_has_User AS gu
WHERE (group.gr_id = gu.Group_id) AND gu.User_id = 1
It gives an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group, Group_has_User AS gu WHERE (group.gr_id = gu.Group_id) AND gu.User_id = 1' at line 1
How should I write it correct?
group is a keyword in SQL. Enclose such names in backticks
FROM `group`, Group_has_User AS gu
group is a keyword in SQL. Try giving your tables more sensible names, or using:
SELECT g.gr_id, g.gr_name, g.gr_description, g.parent_id
FROM `group` g, Group_has_User AS gu
WHERE (g.gr_id = gu.Group_id) AND gu.User_id = 1
Try this. Remove the "AS" keyword after the table name Group_has_User and execute the query
Maybe you must write 'Group', not 'group'.

CodeIgniter Concat

I've spent a few hours staring at this piece of code. Fresh eyes please!
Here is a shortened version of the query:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near
'FROM (`requests` c) JOIN `inventory` d ON `d`.`listing_seq_no` = `c' at line 7
SELECT DISTINCT `c`.`req_id`, `u`.`user_id`, `u`.`org_name`,
CONCAT_WS(' ', `l`.`strength`, `l`.`unit)` as dos, `c`.`quantity` AS quantity1,
(SELECT sum(quantity) from inventory d2
WHERE d2.listing_seq_no = c.listing_seq_no
) as inv_total,
FROM (`requests` c)
JOIN `inventory` d
ON `d`.`listing_seq_no` = `c`.`listing_seq_no`
JOIN `listings` l
ON `l`.`listing_seq_no` = `c`.`listing_seq_no`
EDIT: Original CodeIgniter Code snippet:
$this->db->select ( "c.req_id,
u.user_id,
u.org_name,
l.tradename as name,
CONCAT_WS(' ', l.strength, l.unit) as dos,
);
This:
CONCAT_WS(' ', `l`.`strength`, `l`.`unit)`
Should be:
CONCAT_WS(' ', `l`.`strength`, `l`.`unit`)
try removing the parens around (requests c)
It's an old question, but you can use:
$this->db->select("<your_select_portion>", FALSE);
to avoid the auto-quoting feature of the CI DB class. I am a fan of using MySQL functions on select statements with CI, and this usually fixes the problem (except when you start using the query-caching feature, but that's another story).

Mysql join 2 database and 3 tables query?

SELECT db1_t1.userid as userid
, db1_t1.customer_id as vw_customer
, db2_t1.customers_id as customer
, db2_t1.orders_id as order
FROM database1.table1 db1_t1
LEFT JOIN database2.table1 db2_t1
ON db1_t1.customer_id = db2_t1.customers_id
It gives me this error:
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'order FROM
database1.table1 db1_t1 LEFT JOIN
database2.' at line 2
I am using php and mysql.
order is a keyword - think ORDER BY my_column.
I'd suggest renaming it, but you could enclose it in backticks
db2_t1.orders_id AS `order`