Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hy everyone.
I working an app for android which is query result from database. the android side is OK, i can transfer data in and out from DB server to android. but i have difficulties in PHP and mysql stuff, specifically to find record by field/column value and print the row that matched the value i search.
Let's see this is my database structure :
database name is LBS, table name is information.
ID, NAME, JOB
1, john, teacher
2, doe, farmer
3, obama, farmer
4, sweden, teacher
what syntax i can use to print row that have for example teacher value form JOB column ?, so the output is like this :
1, john, teacher
4, sweden,teacher
Thanks in advance ! :)
Just make a where condition
SELECT * FROM information WHERE job = 'teacher';
Then you will get output as
ID |NAME |JOB
---|-------|------
1 |john |teacher
4 |sweden |teacher
SELECT * FROM information WHERE JOB = 'teacher'
Try for outputting as string:
SELECT CONCAT_WS(',', ID, NAME, JOB) WHERE JOB='teacher'
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a very dumb question.
Lets assume that I have 2 tables, A which has 10 rows and B which has 100 rows.
I know that when I enter SELECT * FROM A, B, then the result would be 1000 rows.
My question is, why?
Can someone please explain this to me?
Because this is a cross join, which gives you a cartesian product between the two tables.
Wikipedia :
CROSS JOIN returns the Cartesian product of rows from tables in the
join. In other words, it will produce rows which combine each row from
the first table with each row from the second table
So in this case, 10 * 100 = (guess what) 1000.
If you want to see 110 resord in the result you should use UNION to combine results from two tables:
(SELECT * from A)
UNION
(SELECT * from B)
You have to integrate a relation with some WHERE, for edit this output
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have a table in my database called users and in this table there is a field called rank. This field holds the id of the users rank.
I also have a table called 'ranks' in my database, and this table has id, rank_id and rank_name, the id being the primary auto increment field, the rank_id hold the rank id and this is what is being used in my users table.
All is good. I need to display the users rank_name, not the rank_id when I output what rank they are. Im not a pro at mysql, i am still learning.
How do i get the users rank, match it up to the rank_id in the table ranks and then output the rank_name from the corresponding rank_id?
Please answer this using mysql_*. I know its life is ending soon but this is what im practicing with at the moment, will help me learn how to convert to mysqli_* or PDO.
You will need to use a query like this
SELECT u.name, r.rank_name
FROM users AS u
LEFT JOIN ranks AS r ON u.rank = r.rank_id
I would suggest you read about the different joins. Also remember you can join multiple tables in this fashion.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm working with a table that contains 50 columns (with bit values), for each 50 U.S. state. I'm trying to find an SQL statement that will return all columns containing a 1. I've looked around for ways of doing this with no luck.
Any help would be much appreciated
If this is a new development, and not for existing tables I would suggest the following. (Otherwise I'm stumped.) It sounds like you have implemented a many-to-many. I would suggest that you implement a table for States as suggested by #juergen d (albeit without the bit field); implement your current entity as-is but without the columns for states. And then implement a third table that has two columns, one for a state's ID and another for the ID of your entity.
Then instead of setting bit fields on your entity's table, you would create entries in this third table.
You can then perform joins on tables to obtain the states set on a certain entity.
For more info see http://en.wikipedia.org/wiki/Many-to-many_(data_model) and http://www.lornajane.net/posts/2011/inner-vs-outer-joins-on-a-many-to-many-relationship
You should better change your table design. Better use something like this
States table
-------------------
id int
name varchar(100)
bitcol bit(1)
Then you could select that states like this
select name from States
where bitcol = 1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Please help me to find next batch number corresponding to product code with using PHP and MySQL. My table is like below given image:
Table Screenshot
If I want to enter new batch of product RM-SO-070G then the PHP form must get new batch number accordingly.
Your SQL should look something like this ...
SELECT `some_tbl`.`batch_no` FROM `some_tbl` WHERE `some_tbl`.`product_code` = ? ORDER BY `some_tbl`.`batch_no` DESC LIMIT 1
And then once you get these results you will need to remove the bt from the front of the batch_no
$next_batch_no = intval(substr($result_row['batch_no'], 2))++;
Create an additional table that will pose as a sequencer - the table will have just one column holding the next batch value. Each time you add a new product to your initial table, you take the batch number from the sequencer table and update the sequencer with the next batch number you want for the next product you add.
Try this:
$product = "RM-SO-070G";
$previous = "bt001";
$query = "SELECT batch_no FROM table WHERE product_code = '".$product."'
AND batch_no = (SELECT MIN(batch_no) FROM table
WHERE batch_no > '".$previous."');";
$result = mysql_query($query);
...
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a database "videos". Which has a table for each video i.e "ID" "Name" "Link" "Count".
i want to be able to take the highest value of any of the tables from the column "Count" and then use which ever has the highest values "Link" Column.
So the best way i can explain is i'm trying to make a "Most Viewed" part of my site so which ever has the highest hit count gets displayed via $row['link'].
Hope this made sense didn't much to me!
Thanks all in advance...
SELECT `id`,`name`,`link`,`count` from my_table order by `count` desc limit 5;
This will give you 5 most viewed links.
Tip : You should not use words like count as column names as they are MySQL keywords.
SELECT `link` FROM `someTable` ORDER BY `Count` DESC LIMIT 1
This will give you the link of the row with the highest count.