How to find table name using select query - mysql

SELECT product FROM Table_name_1
WHERE id = '% textbox1.text %'
UNION
SELECT product FROM Table_name_2
WHERE id = '% textbox1.text %'
UNION
SELECT product FROM Table_name_3
WHERE id = '% textbox1.text %'
Through this code, I am able to get the products. But how will I know from which table this result fetch out? So I want to know table name too.

You can also select constant values and give them a column name.
SELECT product, 'Table_name_1' as table FROM Table_name_1 WHERE id = '% textbox1.text %'
UNION
SELECT product, 'Table_name_2' as table FROM Table_name_2 WHERE id = '% textbox1.text %'
UNION
SELECT product, 'Table_name_3' as table FROM Table_name_3 WHERE id = '% textbox1.text %'

Related

MySQL - SELECT … WHERE id IN (SELECT ...)

I have the following query
SELECT * FROM table WHERE id IN (5,4,3,1,6)
and i want to get this value " 5,4,3,1,6 " from SELECT again.
SELECT * FROM table WHERE id IN (SELECT popuplarList FROM Settinng WHERE id =1 )
But i get only post number 5. I want to get all post from " 5,4,3,1,6 "
Is it possible to solve this with another logic or another way ?
thanks a lot
Try FIND_IN_SET Method
SELECT * FROM table WHERE FIND_IN_SET(id, (SELECT popuplarList FROM Settinng WHERE id =1 ))
Please try below:
1st solution:
select *
from table
where (
select CONCAT(",", popuplarList, ",")
from Settinng where id = 1
) like concat("%,", id, ",%")
2nd solution:
select * from table WHERE FIND_IN_SET(`id`, (
select CONCAT(",", popuplarList, ",") from Settinng where id = 1)
)

How to split the string in a single column and arrange the same in the same column in SQL

I have a column as below
Products
jeans,oil
jeans,shampoo
I want to split the strings and use it in the same column using SQL. The result I want is
Products count
jeans 2
oil 1
shampoo 1
Could you please guide me in getting this result
Thank you
You are storing CSV data in your SQL table, which is not a good thing. But it looks like you are trying to move away from that, which is a good thing. Here is one option using a union with SUBSTRING_INDEX:
SELECT Products, COUNT(*) AS count
FROM
(
SELECT SUBSTRING_INDEX(Products, ',', 1) AS Products FROM yourTable
UNION ALL
SELECT SUBSTRING_INDEX(Products, ',', -1) FROM yourTable
) t
GROUP BY Products
ORDER BY
count DESC, Products;
Demo
Firstly you need to split the data into two columns like
SELECT CASE
WHEN name LIKE '%,%' THEN LEFT(name, Charindex(' ', products) - 1)
ELSE name
END,
CASE
WHEN name LIKE '%,%' THEN RIGHT(name, Charindex(' ', Reverse(products)) - 1)
END
FROM YourTable
then you need to union this with the same table... and the final code will look like...
select count( distinct abc), abc from
(
SELECT CASE
WHEN PA_NAME LIKE '% %' THEN LEFT(PA_NAME, Charindex(' ', PA_NAME) - 1)
ELSE PA_NAME
END [abc]
FROM phparty
union all
SELECT CASE
WHEN PA_NAME LIKE '% %' THEN RIGHT(PA_NAME, Charindex(' ', Reverse(PA_NAME)) -1)
END [abc]
FROM phparty
) t group by abc
here you can replace pa_name with your_column_name

MySQL 'AND' clause does not operate as it should

I am trying to create a search engine system. In the MySQL statement, I have concatenated first name and last name and trying to get users which matches the key letters in their first name and last name. If their account_type = 0 and account_ban = 0, it will also return users with account_Type = 1 and account_ban = 1. Following is my query:
SELECT
firstname, lastname
FROM users
WHERE CONCAT(firstname,lastname) LIKE '%jb%'
OR CONCAT(firstname,lastname) LIKE '%bj%'
AND account_type = 0
AND account_ban = 0;
I have two problem here. First one that it returns users with account_type = 1 and account_ban = 1 also and second problem is that if I insert a space in the name e.g. %j b% or %b j%, it will not return anything. Please help.
It is unclear what you really want in terms of the spaces, but here is one solution:
SELECT firstname, lastname
FROM users
WHERE (CONCAT(firstname, lastname) LIKE '%j%b%' OR
CONCAT(firstname, lastname) LIKE '%b%j%'
) AND
account_type = 0 AND account_ban = 0;
This will match any character between the "b" and the "j", including no characters and a space. You can match exactly one character with '_', but that doesn't seem to be what you want. This should do the trick:
WHERE (REPLACE(CONCAT(firstname, lastname), ' ', '') LIKE '%j%b%' OR
REPLACE(CONCAT(firstname, lastname), ' ', '') LIKE '%b%j%'
) AND
account_type = 0 AND account_ban = 0;
Because AND will evaluate before OR (thats a matematically problem) you have to make parenthesis around the OR statement:
SELECT
firstname
, lastname
FROM users
WHERE
( CONCAT(firstname,lastname)
LIKE '%jb%'
OR
CONCAT(firstname,lastname)
LIKE '%bj%'
)AND
account_type = 0
AND
account_ban = 0
;
You need to add parenthesis to filter proper records.
To get account_type = 1 and account_ban = 1 you need add that condition in where clause. Try this.
SELECT firstname,
lastname
FROM users
WHERE ( Concat(firstname, lastname) LIKE '%jb%'
OR Concat(firstname, lastname) LIKE '%bj%' )
AND ( ( account_type = 0
AND account_ban = 0 )
OR ( account_type = 1
AND account_ban = 1 ) )
I think you forgot some brackets:
SELECT firstname, lastname
FROM users
WHERE (CONCAT(firstname,lastname) LIKE '%jb%'
OR CONCAT(firstname,lastname) LIKE '%bj%')
AND account_type = 0
AND account_ban = 0;
This should solve your first problem.
Use proper parenthesis will solve your problem.
Your query should look like this:
SELECT firstname, lastname
FROM users
WHERE (CONCAT(firstname,lastname) LIKE '%jb%'
OR CONCAT(firstname,lastname) LIKE '%bj%')
AND account_type = 0
AND account_ban = 0;

Where mistake in sql LIKE clause?

I try to count all items from another table with this select:
SELECT id, name, (SELECT count(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
and prekes_main.pg_kodas LIKE 'grupes_main.pg_kodas%') as pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
and grupes_main.name <> ''
In LIKE clause I want automatically get selected grupes_main column pg_kodas, but in this query it always returns 0, where is mistake in LIKE function? thx
SELECT id, name,
(
SELECT COUNT(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
AND prekes_main.pg_kodas LIKE CONCAT(grupes_main.pg_kodas, '%')
) pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
AND grupes_main.name <> ''

SQL QUERY NOT IN

SELECT COUNT(accessed_time) AS total
FROM user_db
WHERE application_id LIKE '1%'
AND accessed_time BETWEEN '" + date1 +"' AND '"+daten+"'
This query will fetch all the application id that begin with 1...i need to exclude 101....from result...how can i modify the query
Simply add a AND.
SELECT COUNT(accessed_time) AS total
FROM user_db
WHERE application_id LIKE '1%'
AND application_id <> 101
AND accessed_time BETWEEN '" + date1 +"' AND '"+daten+"'
SELECT COUNT(accessed_time) AS total
FROM user_db
WHERE application_id LIKE '1%' AND accessed_time BETWEEN '" + date1 +"' AND '"+daten+"'
AND application_id <> '101'