i have two tables
tb1
tb1_id - store_ids - date
1 - 1,2,3,4 - 2023-01-01
2 - 3,4 - 2023-06-01
tb2
tb2_id - name - date
1 - gold - 2023-01-01
2 - mond - 2023-01-01
3 - burgar - 2023-01-01
4 - glass - 2023-01-01
5 - blackD - 2023-01-01
what i have tried is
sql
SELECT *
FROM `tb2`
JOIN `tb1`
WHERE `tb2_id` IN (`store_ids`)
and i get error
'Warning: #1292 Truncated incorrect INTEGER value: 1,2,3,4'
You can use find_in_set
select * from tb1 join tb2 on find_in_set(tb2_id ,tbl1_id)
But as I mentioned in my earlier comment, it is better to redesign your table
DEMO
You could try below query
SELECT *
FROM `tb2`
JOIN `tb1`
WHERE `store_ids` REGEXP CONCAT('[[:<:]]',`tb2_id`,'[[:>:]]') -- MySQL 5.6
-- For MySQL 8.0, using WHERE `store_id` REGEXP CONCAT('\\b',`tb2_id`,'\\b')
But it's better to not store foreign keys as list of ids separated by comma.
Related
I have a some entries in database table rows as follows.
101 - 1
101 - 2
101 - 3
102 - 1
102 - 2
102 - 3
103
I need to get the result of SELECT Query for count as '3' since there are 101 and 102 are the only number before the -.
So is there any way to find the unique value in db table columns before a character?
EDIT : I have entries even without the - .
In case your entries have always the format you have provided us, you just have to find the position of the '-' character, split the values, get the first n characters and count the distinct values
This works for SQL Server, otherwise informs us about what DBMS you are using or replace the functions with the ones of your DBMS on your own
SELECT COUNT(DISTINCT SUBSTRING(val,0,CHARINDEX('-', val))) from YourTable
create table T1
(
id int primary key identity,
col1 varchar(20)
)
insert into T1 values('101 - 1'),('101 - 2'),('101 - 3'),('102 - 1'),('102 - 2'),('102 - 3')
select SUBSTRING(col1,0,CHARINDEX(' ',col1)) as 'Value',count(*) as 'Count' from T1 group by SUBSTRING(col1,0,CHARINDEX(' ',col1))
I have a table like these
date - name - val
16/02 - Rossi - 5
16/02 - Zingaro - 8
18/02 - Beniamino - 4
18/02 - Bosso - 2
Is possible with a query to get result like this?
16/02
Rossi - 5
Zingaro - 8
18/02
Beniamino - 4
Bosso - 2
Or I must do the query and then work with if/else ?
Simple select query can be like
select name,val froom table where date=&date
so with this you will get all the record with name & val for particular date.
Now question is no one is going to write query for each date.
So simply you can fetch all distinct date from your table with query
select DISTINCT date from table name
Now what remains is that you need to write a procedure which will have a loop and will take output of ditinct date as iterator and for each iteration fire select query for name & val.
Is it possible to manipulate column data in a query where you do SELECT *?
table
id - delivery
1 - 0
2 - 0
3 - 12
Something like:
SELECT *, IF(delivery = '0', '9') FROM table
Or do you have to select the columns individually?
This would be a total mess for me since the actual table has like 40 columns.
EDIT: I didnt make myself clear what i wanted. The result should be:
id - delivery
1 - 9
2 - 9
3 - 12
Try this:
SELECT *, case delivery when '0' THEN '9' ELSE delivery END FROM table
Try this
,
Select
column1,
column2,
CASE WHEN delivery=0 THEN 9
ELSE delivery
END DELIVERY
from TABLE
I Have a sitatation like the following:
In MySql Table Entries:
img_id name
1 aa.jpg
2 aab.mpeg
3 aabc.jpg
4 aabd.jpg
5 aabn.jpg
6 aabf.jpg
7 aadf.jpg
8 aacf.jpg
I want the count after splitting thew above values........
like
".jpg"=>7
".mpeg"=>1
SELECT RIGHT(Name, LOCATE('.', REVERSE(Name)) - 1) Format,
COUNT(*) TotalCOunt
FROM TableName
GROUP BY RIGHT(Name, LOCATE('.', REVERSE(Name)) - 1)
SQLFiddle Demo
Consider normalizing the table. In the long run, this will perform slow.
This is my table definition:
CREATE TABLE orders_total (
orders_total_id int unsigned NOT NULL auto_increment,
orders_id int NOT NULL,
title varchar(255) NOT NULL,
text varchar(255) NOT NULL,
value decimal(15,4) NOT NULL,
class varchar(32) NOT NULL,
sort_order int NOT NULL,
PRIMARY KEY (orders_total_id),
KEY idx_orders_total_orders_id (orders_id)
);
A populated table can look something like this (all made-up numbers from my memory):
1 - 13 - Shipping - $7.50 - 7.50 - ot_shipping - 2
2 - 13 - Tax - $8.00 - 8.00 - ot_tax - 4
3 - 13 - Total - $56.67 - 56.67 - ot_total - 3
4 - 14 - Shipping - $5.55 - 5.55 - ot_shipping - 2
5 - 14 - Discount - $6.40 - 6.40 - ot_discount - 1
6 - 14 - Coupon - $10.00 - 10.00 - ot_coupon - 5
7 - 14 - Total - $150.25 - 150.25 - ot_total - 3
Every order can have different classes, and not all orders have to have the exact number of classes. For example:
Order 1 can have the following classes: ot_shipping, ot_tax, ot_total
Order 2 can have the following classes: ot_shipping, ot_discount, ot_coupon, ot_total
...
I'd like to write a query that queries my orders table (not displayed here), and joins the above table (orders_total), and grabs the "value" column only if the "class" is either "ot_total", "ot_coupon", or "ot_discount".
Here is my attempt at creating such a query:
SELECT o.orders_id, o.customers_name, group_concat(ot.value) AS values
FROM orders o
LEFT JOIN orders_total ot
ON ot.orders_id = o.orders.id
GROUP BY o.orders_id
The above query would produce something like this:
First set of results:
orders_id => 13
customers_name => John Doe
values => 7.50,8.00,56.67
Second set of results:
orders_id => 14
customers_name => Jane Roe
values => 5.55,6.40,10.00,150.25
As you can see, the values gives me what I need, but I just don't know what number belongs to which class. I'm only interested in ot_total, ot_discount, and ot_coupon.
Anybody can think of any solution?
Is it possible to somehow append the class name to the values as well? So I can have something like values => ot_shippimh7.50,ot_tax8.00,ot_total56.67
You can do this:
SELECT o.orders_id, o.customers_name,
GROUP_CONCAT(CONCAT( ot.class, ot.value)) AS values
FROM orders o
LEFT JOIN orders_total ot
ON ot.orders_id = o.orders.id
WHERE ot.class in ("ot_total", "ot_coupon", "ot_discount")
GROUP BY o.orders_id
BTW: You can do better, by remove the class field from your orders_total, add a new table called Classes instead that contains a list of classes then add a foreign key reference to this table to your orders_total.