using the value of mysql column to specify table name - php

Suppose I wanna do a join between table 1 and other tables...table 1 contains a column that specifies which table that row should be joined with
eg:
Table 1:
entry | tableName
333 | table3
4444 | table2
111 | table3
so 333 should be joined with table3, 4444 with table2, etc....
is there a way to specify mysql queries to use the column values like this as the name of the table to be joined with the entry?

You will have to fetch the result set from this table where you have the entry, table details & build a dynamic query. You will not be able to do this in a single query.
Not sure why you have such requirement. It's always best to know what are the tables & relationships to get the best possible join.

You can only do that if you build a dynamic query.

Related

Get all row had value in column

How do I get all rows having the value 1, but not 11 in column news_short?
Here's my table:
id | news_short |
1 | 1,2,3,4,5,6,7 |
2 | 2,4,5,6,1,5,6 |
3 | 11,2,5,6,9,4 |
I agree you should normalize. But here is the solution the way it sits.
Select * from table where news_short like '%1,%' and
news_short not like '%11,%';
FIND_IN_SET() returns the position of a string if it is present (as a substring) within a list of strings
so you should search if a value is != 11
eg:
->where("FIND_IN_SET(news_short) !=", 11)
Your best bet would be normalize your schema do not store relations in form of comma separated list instead create a junction table to maintain a m:m many to many or if its one to many relation between main table and these news_short (probably from other table) values,create a new table as news_short with columns main table id and news(table) id and in each row save one association per your_table and news.
If you aren't able to update/change your schema you could use find_in_set() but its not a good option
select *
from your_table
where find_in_set(1, news_short) > 0
demo

Join the values of 2 tables

I have 2 tables and I can't merge the values of these tables into table2.
Table1:
id | studnum | fname | lname | mname
1 | 1001 | Mark | Lei | Ramos
Table2:
id | studnum | remarks
Is it possible that when I input the values into Table2, the studnum from Table1 will input into Table2 also?
I tried this but it doesn't worked.
$sql = "SELECT table1.studnum FROM table1, table2
WHERE table1.studnum = table2.studnum";
So that the Table2 will be like this output
id | studnum | remarks
1 | 1001 | good
What you're looking for is using a JOIN Statement.
If the shared key is between them is studnum, you can do
SELECT * FROM table1
JOIN table2
ON table1.studnum = table2.studnum
Try a MySQL join function. Example:
SELECT *
FROM table1
JOIN table2
ON table1.studnum=table2.studnum;
More reading on joins: http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php
If those are exactly your tables you dont really need what you are requesting as it goes against normalization.
In the case you will only have one or none remarks for an entry in table 1 there is no need for table 2, the remarks should be in table 1.
In the case you need multiple remarks for a single entry in table 1, you dont need stdnum in table 2. If your problem in this scenario is to construct a query to get the remakrs based on stdnums you can easily achieve this with a joint.
The only field you should be duplicating from table to table for a record/object is the id. You can (and may be should) scrifice normalization in pro of efficiency but your example is not the case.

change a table`s status to updated on inserting new values to another table in mysql

I've got two tables which are related to each other. I want to change table 1 status to updated when I insert new values into table 2 and return mysqli_affected_rows() for table 1
table 1: products
id | name
1 | product
table 1: categories
id | name | product_id
1 | cat 1 | 1
2 | cat 2 | 1
is this possible?!
You show the structure of the tables, but don't include any column that should be updated. This makes me think that you don't really want an update. You just want a query that returns whether a given product is in the categories table.
If so, the following may do what you want:
select p.*,
coalesce( (select 1 from categories c where c.productid = p.id limit 1), 0) as InCategories
from products p;
If you try to go another route where you actually store the value in the table, you will need to use a stored procedure or triggers. Do consider, though, what happens when you delete or update a row. insert is not the only way to modify data in a table.

How to match CSV value in column

my mysql table name products.
How can get data this table.
table shema
id| catid |subcatid
-----------------
1 | 5 | 2,3,5
SELECT * FROM products where subcatid=2
not correct
how can write correct sql.
SELECT * FROM products where find_in_set(2,subcatid)
consider to normalize your table

Compare two tables and update second table

There are two tables table1 and table2
table1 has got two columns name and rank
table2 has got only one column name
names in table2 are almost listed in table1
I want compare two table and pull rank info from table1 and update/alter table2 with rank
table1
name | rank
-------------
john | 2
mathews| 5
keyn | 4
emly | 25
yancy | 8
stewart| 9
kim | 12
chris | 19
table2
name
-------
john
mathews
keyn
emly
yancy
stewart
I want update/insert rank details to table2 from table1
thats it and sorry for the confusion
Seem like you want to do something like this:
update table2,table1 set table2.rank=table1.rank where table2.name=table1.name
This will update the second table with ranks from the first table where the names are equal.
Then put auto increament field of table one in table2. and after this apply left join using these id and pull the info
refer this link
http://www.wellho.net/solutions/mysql-left-joins-to-link-three-or-more-tables.html

Categories